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
//Triangle.java
import java.util.*;
public class Triangle
{
double side1, side2, side3;
public Triangle()
{
side1 = 1;
side2 = 2;
side3 = 3;
}
public void buildTriangle()
{
Scanner keyboard = new Scanner(System.in);
System.out.print(\"Please enter the length of side 1:\");
side1 = keyboard.nextDouble();
System.out.print(\"Please enter the length of side 2:\");
side2 = keyboard.nextDouble();
System.out.print(\"Please enter the length of side 3:\");
side3 = keyboard.nextDouble();
keyboard.nextLine();
}
public void classifyTriangle()
{
boolean isEquilateral = false;
if((side1 > side2+side3) || (side2 > side1+side3) || (side3 > side1+side2))
{
System.out.println(\"This is not a triangle\");
System.exit(0);
}
else
{
System.out.println(\"This is a triangle\");
}
if((side1*side1 == (side3*side3)+(side2*side2)) || (side2*side2 == (side3*side3)+(side1*side1)) || (side3*side3 == (side1*side1)+(side2*side2)))
{
System.out.println(\"This is a right triangle\");
}
else
{
System.out.println(\"This is not a right triangle\");
}
if((side1 == side2) && (side2 == side3))
{
System.out.println(\"This is an equilateral triangle\");
isEquilateral = true;
}
else
{
System.out.println(\"This is not an equilateral triangle\");
}
if(!isEquilateral && (side1 == side2) || (side2 == side3) || (side3 == side1))
{
System.out.println(\"This is an isosceles triangle\");
}
else
{
System.out.println(\"This is not an isosceles triangle\");
}
}
public double perimeter()
{
return side1 + side2 + side3;
}
public double area()
{
double p = (side1 + side2 + side3)/2.0;
double a = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return a;
}
}
//TestTriangle.java
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(\"The area of the triangle is \" + prettyArea + \".\");
System.out.println(\"The perimeter of the triangle is \" + prettyPerim + \".\");
}
}
/*
output:
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 triangle is
This is a triangle
This is a right triangle
This is not an equilateral triangle
This is not an isosceles triangle
The area of the triangle is 6.00.
The perimeter of the triangle is 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 triangle is
This is not a triangle
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 triangle is
This is a triangle
This is not a right triangle
This is not an equilateral triangle
This is an isosceles triangle
The area of the triangle is 17.9.
The perimeter of the triangle is 20.0.
The perimeter of the triangle is 19.0.
*/




