Write a separate Java class for a triangle named YourName_Triangle that will hav
ID: 3768990 • Letter: W
Question
Write a separate Java class for a triangle named YourName_Triangle that will have three private instance variables of type double for the lengths of the sides of a triangle. Note that perimeter, area and semi-perimeter are NOT private variables in this class. This program uses Heron's formula (Math.sqrt(s*(s-a)*(s-b)*(s-c)). "s" being semiperimeter, and three sides being a,b,c to calculate the area of triangle, rather than base or height. There is no need of using booleans. Triangle can be imperfect. The Java class should contain these methods:
A constructor that initializes the instance variables to default values of 0. This Constructor has no parameters
A constructor with three parameters that initializes the instance variables to the parameter values. Note this constructor is not used in the assignment.
A method for the user to input the lengths of the sides of a triangle. Include prompts. Allow values only greater than 0
A method that outputs the lengths of the sides of the triangle along with a message
A method that returns the perimeter of the triangle
A method that returns the area of the triangle
This class should be placed in a file that is separate from the class containing the main program. Note: Only method #2 above has parameters. As is typical in a class, the rest of the class methods do not have parameters.
Write a Java application main program in Java file YourName in the order specified below. This code will be in a .java file that is separate from the one for the Triangle class above. Note that the main method should have perimeter and area as local variables. It should NOT have declarations for the sides or semi-perimeter.
You can use global scope only for the Toolkit. All other declarations, including creating an instance of the triangle class, should be local to the main program. (If necessary, of course, you can declare local variables in the main program’s methods as needed.)
Call a void method in the main program that outputs an explanation of what the program does. Include in the explanation that this program uses a class for the sides of a triangle and include your name.
Get the lengths of the sides from the user. Use the method from the Triangle class.
Calculate the perimeter of the triangle. Use the method from the Triangle class.
Calculate the area of the triangle. Use the method from the Triangle class.
Use a void method to output, with appropriate messages: The lengths of the sides of the triangle (use the method from the Triangle class), the perimeter of the triangle, and the area of the triangle.
The style of your code should be similar to that of Interactive Average with a Class. Be sure to include comments – programmer identification, assignment number, section number as appropriate, purpose, and a brief explanation of the methods should appear in the header comment, along with the vocabulary word and quote. Include an explanation for all variables, parameters and methods.
Explanation / Answer
triangle.java
import java.util.Scanner;
class triangle
{
private double a;
private double b;
private double c;
public double s;
public double perimeter;
public triangle()
{
a=0;
b=0;
c=0;
s=0;
perimeter=0;
}
public triangle(double A, double B, double C)
{
a=A;
b=B;
c=C;
s=(A+B+C)/2;
perimeter= (A+B+C);
}
public void setValues()
{
Scanner in = new Scanner(System.in);
do{
System.out.println("enter first side of triangle");
a=in.nextDouble();
if(a<=0)
System.out.println("side of a triangle should be greater than zero");
}while(a<=0);
do{
System.out.println("enter second side of triangle");
b=in.nextDouble();
if(b<=0)
System.out.println("side of a triangle should be greater than zero");
}while(b<=0);
do{
System.out.println("enter third side of triangle");
c=in.nextDouble();
if(c<=0)
System.out.println("side of a triangle should be greater than zero");
}while(c<=0);
s=(a+b+c)/2;
perimeter=(a+b+c);
}
public void displaySides()
{
System.out.println("First side = "+a);
System.out.println("Second side = "+b);
System.out.println("Third side = "+c);
}
public double perimeter()
{
return perimeter;
}
public double area()
{
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}
TestClass.java
class TestClass {
public static void main(String args[] ) throws Exception {
message();
triangle t = new triangle();
t.setValues();
double perimeter = t.perimeter;
double area = t.area();
System.out.println("perimeter = "+perimeter);
System.out.println("area = "+area);
System.out.println("Hello World!");
}
public static void message()
{
System.out.println("This program is used to test a triangle class that takes sides and calculate its output using Heron's formula");
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.