Overview Create a Java class called Triangle that when used
Overview:
Create a Java class called Triangle that when used with the supplied driver TestTriangle.java displays some properties of your triangle.
Do not change any of the code in TestTriangle.java.
Specications
Create a Java class le called Triangle.java that creates a Triangle object that stores the following information:
• Length of side 1,
• Length of side 2,
• Length of side 3, and
• A scanner object for input.
When the buildTriangle() method is invoked, ask the user to input the lengths of the three sides of a triangle. After receiving the three values, your program should provide answers for each of the following questions:
• Is it really a triangle? If one side is longer than the sum of the other two sides, then it is not.
• Is it a right triangle? That is, does it satisfy the Pythagorean Theorem?
• Is it an equilateral triangle? That is, are all three sides the same?
• Is it an isosceles triangle? That is, are two sides the same? (If your program has already determined that the triangle is equilateral, then it should NOT state that it is isosceles.)
• What is the area of the triangle? Use ps(s a)(s b)(s c), where s =1/ 2 perimeter.
• What is the perimeter of the triangle?
Notes/Hints
You should break up your program into multiple methods. I have provided you with templates for
• The default constructor Triangle()
• The public method buildTriangle()
• The private method getSide(int index)
It remains for you to write the code for the following public methods
• classifyTriangle()
• perimeter()
• area()
Below are some general hints/tips for completing this project.
• It may be easiest to deal with sorted values in the classifyTriangle() method. I recommend sorting the values in the
buildTriangle() method after you have received a value for each of the sides.
• Real numbers should be represented as doubles
• Java has a built in function called Math.sqrt()
EXAMPLE OUTPUT
Welcome to the Triangle Program
Please enter the length of side 1: bob
You did not enter a valid input . Exiting .
Welcome to the Triangle Program
Please enter the length of side 1: 3
Please enter the length of side 2: 4
Please enter the length of side 3: 5
My analysis of this t r i a n g l e i s
This i s a right t r i a n g l e
The area of the t r i a n g l e i s 6.00.
The perimeter of the t r i a n g l e i s 12.0.
Welcome to the Triangle Program
Please enter the length of side 1: 10
Please enter the length of side 2: 2
Please enter the length of side 3: 7
My analysis of this t r i a n g l e i s
Not a t r i a n g l e
Welcome to the Triangle Program
Please enter the length of side 1: 6
Please enter the length of side 2: 6
Please enter the length of side 3: 8
My analysis of this t r i a n g l e i s
This i s NOT a right t r i a n g l e
This i s an i s o s c e l e s t r i a n g l e
The area of the t r i a n g l e i s 17.9.
The perimeter of the t r i a n g l e i s 20.0.
Welcome to the Triangle Program
Please enter the length of side 1: 4
Please enter the length of side 2: 9
Please enter the length of side 3: 6
My analysis of this t r i a n g l e i s
This i s NOT a right t r i a n g l e
The area of the t r i a n g l e i s 9.56.
The perimeter of the t r i a n g l e i s 19.0.
IMPORTANT:
Below is the code needed for TestTriangle.java
This code must be used in this program
public class TestTriangle {
public static void main(String[] args) {
System.out.println(\"Welcome to the Triangle Program\");
System.out.println();
Triangle myTriangle = new Triangle();
myTriangle.buildTriangle();
System.out.println();
System.out.println(\"My analysis of this triangle is\");
System.out.println();
myTriangle.classifyTriangle();
String prettyPerim = String.format(\"%.3g\", myTriangle.perimeter());
String prettyArea = String.format(\"%.3g\", myTriangle.area());
System.out.println(\"\\tThe area of the triangle is \" + prettyArea + \".\");
System.out.println(\"\\tThe perimeter of the triangle is \" + prettyPerim + \".\");
}
Solution
public class Triangle
{
private double side1;
private double side2
private double side3;
private Scanner s;
Triangle()
{}
public void buildTriangle()
{
s=new Scanner(System.in);
System.out.println(\"enter side 1 length :\");
side1=s.nextDouble();
System.out.println(\"enter side2 length :\");
side2=s.nextDouble();
System.out.println(\"enter side3 length :\");
side3=s.nextDouble();
}
public void classifyTriangle()
{
if(side1==side2&&side2==side3)
System.out.println(\"equilateral triangle\");
else if((side1*s ide1==side2*side2+side3*side3)||(side2*s ide2==side1*s ide1+side3*side3)||(side3*s ide3<=side1*s ide1+side2*side2))
System.out.println(\"right angled triangle \");
else if(side1==side2||side2==side3||side3==side1)
System.out.println(\"isosceles triangle\");
else if((side1<=side2+side3)||(side2<=side1+side3)||(side3<=side1+side2))
System.out.println(\"scalene triangle \");
else
System.out.println(\"not a triangle\");
}
public double perimeter()
{
return side1+side2+side3;
}
public double area()
{
double s=(side1+side2+side3)/2;
return sqrt(s*(s-side1)*(s-side2)*(s-side3);
}
public void getSide(int index)
{
s=new Scanner(System.in);
System.out.println(\"enter length of side \"+index+\" : \");
if(index==1)
side1=s.nextDouble();
if(index==2)
side2=s.nextDouble();
if(index==3)
side3=s.nextDouble();
}
}



