In: Computer Science
Java
Write a class called Triangle that can be used to represent a triangle.
Write a class called Describe that will interface with the Triangle class
The Server
• A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created.
• It will allow a client to create a Triangle, passing in integer values for the three sides. If the values for the sides that are passed in do not represent a valid Triangle, then all sides will be set to a value of 1. The constructor should add 1 to the count of the number of Triangles created and also call a method to calculate the perimeter and then add the perimeter for that object to an accumulator.
• It will contain a separate private method called isValid(). The sum of any two sides of a triangle must be greater than the third in order to represent a valid triangle. Also, no side may be 0 or negative.
• The Triangle class must have the following methods that return a Boolean value: • isRight () - a right triangle • isScalene() - no two sides are the same length
• isIsosceles() - exactly two sides are the same length
• isEquilateral() - all three sides are the same length • isSimilar(Triangle t) - corresponding sides of similar triangles have lengths that are in the same proportion.
• equals (Triangle t) – compares two Triangle objects to determine if they are equal. We will compare their perimeters to determine equality.
• In addition, the Triangle class will need the following methods:
o toString() – returns a String that specifies the values for the 3 sides of the triangle
o calcPerim() – calculate and return the perimeter of the object o isValid() – returns a Boolean based on the criteria described above.
o addTotalPerim(). This method will call calc_perim() and add the perimeter for that object to an accumulator.
o reduceTotalPerim(). This method should subtract the perimeter for that object from the accumulator.
o Accessor and mutator methods for all properties
o Be sure to specify proper visibility for all methods and properties
The Client
• The Describe class will contain main()
• It will instantiate 5 Triangles
• tri1 with sides 3, 4, 5
• tri2 with sides 3, 3, 3
• tri3 with sides 4, 4, 5
• tri4 with sides 1, 1, 2
• tri5 with sides -3, 4, 5
• Allow the user to type 3 positive integers on separate lines for each triangle.
• Instantiate five separate objects of the Triangle class (tri_1…tri_5).
• Use if statements to test each triangle for its properties and print appropriate output.
• Print the values of the sides of each triangle.
• Print the properties that apply for each triangle. See the sample output.
• Print the Perimeter for each triangle.
• Test your equals method.
• Test your isSimilar method.
• At the end print the total of the perimeters for all triangles.
• Output must be labeled, aligned, and reasonably well-spaced
·Since arrays haven't been discussed in class, do not try to use a loop to instantiate these objects. There will be a lot of duplicate code in Describe. One approach is to create your first triangle, test for the various properties, and display its output. Then simply copy and paste that code replacing tri1 with tri2. Do the same for all 5 triangles.
// Triangle class that specifies properties and methods for
triangles
public class Triangle {
private int side1;
private int side2;
private int side3;
private static int num_triangles=0;
private static int totalPerimeter=0;
public Triangle(int side1, int side2, int side3)
{
num_triangles++;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
if(!isValid())
{
this.side1=1;
this.side2=1;
this.side3=1;
}
totalPerimeter +=
calcPerim();
}
private boolean isValid()
{
if(side1 <= 0 || side2 <= 0
|| side3 <=0 )
return
false;
if(((side1+side2) <= side3) ||
((side1+side3) <= side2) || ((side2 + side3) <= side1))
return
false;
return true;
}
public boolean isRight()
{
if(((Math.pow(side1, 2) +
Math.pow(side2, 2)) == Math.pow(side3,2)) || ((Math.pow(side2, 2) +
Math.pow(side3, 2)) == Math.pow(side1,2)) ||
((Math.pow(side1, 2) + Math.pow(side3, 2)) ==
Math.pow(side2,2)))
return
true;
return false;
}
public boolean isScalene()
{
if(side1 == side2 || side1 == side3
|| side2==side3)
return
false;
return true;
}
public boolean isIsosceles()
{
if((side1 == side2) &&
(side1 != side3))
return
true;
if((side1 == side3) &&
(side1 != side2))
return
true;
if((side2 == side3) &&
(side2 != side3))
return
true;
return false;
}
public boolean isEquilateral()
{
if((side1 == side2) &&
(side1 == side3))
return
true;
return false;
}
public String toString()
{
return("Side a : "+side1+" Side b :
"+side2+" Side c : "+side3);
}
public boolean isSimilar(Triangle second)
{
return((((double)(side1)/second.getSide1()) ==
((double)(side2)/second.getSide2())) &&
(((double)(side1)/second.getSide1()) ==
(((double)side3)/second.getSide3())));
}
public boolean equals(Triangle second)
{
if(calcPerim() ==
second.calcPerim())
return
true;
return false;
}
public int calcPerim()
{
return(side1+side2+side3);
}
public void addTotalPerim()
{
totalPerimeter +=
calcPerim();
}
public void reduceTotalPerim()
{
totalPerimeter -=
calcPerim();
}
// getters
public int getSide1()
{
return side1;
}
public int getSide2()
{
return side2;
}
public int getSide3()
{
return side3;
}
public static int getTriangles()
{
return num_triangles;
}
public static int getTotalPerimeter()
{
return totalPerimeter;
}
// setters
public void setSide1(int side1)
{
this.side1 = side1;
}
public void setSide2(int side2)
{
this.side2 = side2;
}
public void setSide3(int side3)
{
this.side3 = side3;
}
}
//end of Triangle.java
// Describe.java implementing the Triangle class
import java.util.Scanner;
public class Describe {
public static void main(String[] args) {
int a,b,c;
Triangle tri1 , tri2, tri3, tri4,
tri5;
Scanner scan = new
Scanner(System.in);
System.out.print("Enter an integer
dimension for side a of triangle 1 : ");
a = scan.nextInt();
System.out.print("Enter an integer
dimension for side b of triangle 1 : ");
b = scan.nextInt();
System.out.print("Enter an integer
dimension for side c of triangle 1 : ");
c = scan.nextInt();
tri1 = new Triangle(a,b,c);
System.out.print("Enter an integer
dimension for side a of triangle 2 : ");
a = scan.nextInt();
System.out.print("Enter an integer
dimension for side b of triangle 2 : ");
b = scan.nextInt();
System.out.print("Enter an integer
dimension for side c of triangle 2 : ");
c = scan.nextInt();
tri2 = new Triangle(a,b,c);
System.out.print("Enter an integer
dimension for side a of triangle 3 : ");
a = scan.nextInt();
System.out.print("Enter an integer
dimension for side b of triangle 3 : ");
b = scan.nextInt();
System.out.print("Enter an integer
dimension for side c of triangle 3 : ");
c = scan.nextInt();
tri3 = new Triangle(a,b,c);
System.out.print("Enter an integer
dimension for side a of triangle 4 : ");
a = scan.nextInt();
System.out.print("Enter an integer
dimension for side b of triangle 4 : ");
b = scan.nextInt();
System.out.print("Enter an integer
dimension for side c of triangle 4 : ");
c = scan.nextInt();
tri4 = new Triangle(a,b,c);
System.out.print("Enter an integer
dimension for side a of triangle 5 : ");
a = scan.nextInt();
System.out.print("Enter an integer
dimension for side b of triangle 5 : ");
b = scan.nextInt();
System.out.print("Enter an integer
dimension for side c of triangle 5 : ");
c = scan.nextInt();
tri5 = new Triangle(a,b,c);
System.out.println("\nTriangle 1
has sides of :"+tri1);
System.out.println("The perimeter
is "+tri1.calcPerim()+"\nTriangle Properties : ");
if(tri1.isEquilateral())
System.out.println("\t\tEquilateral");
else if(tri1.isIsosceles())
System.out.println("\t\tIsosceles");
else if(tri1.isScalene()) {
System.out.println("\t\tScalene");
}
if(tri1.isRight())
System.out.println("\t\tRight Triangle");
System.out.println("\nTriangle 2
has sides of :"+tri2);
System.out.println("The perimeter
is "+tri2.calcPerim()+"\nTriangle Properties : ");
if(tri2.isEquilateral())
System.out.println("\t\tEquilateral");
else if(tri2.isIsosceles())
System.out.println("\t\tIsosceles");
else if(tri2.isScalene()) {
System.out.println("\t\tScalene");
}
if(tri2.isRight())
System.out.println("\t\tRight Triangle");
System.out.println("\nTriangle 3
has sides of :"+tri3);
System.out.println("The perimeter
is "+tri3.calcPerim()+"\nTriangle Properties : ");
if(tri3.isEquilateral())
System.out.println("\t\tEquilateral");
else if(tri3.isIsosceles())
System.out.println("\t\tIsosceles");
else if(tri3.isScalene()) {
System.out.println("\t\tScalene");
}
if(tri3.isRight())
System.out.println("\t\tRight Triangle");
System.out.println("\nTriangle 4
has sides of :"+tri4);
System.out.println("The perimeter
is "+tri4.calcPerim()+"\nTriangle Properties : ");
if(tri4.isEquilateral())
System.out.println("\t\tEquilateral");
else if(tri4.isIsosceles())
System.out.println("\t\tIsosceles");
else if(tri4.isScalene()) {
System.out.println("\t\tScalene");
}
if(tri4.isRight())
System.out.println("\t\tRight Triangle");
System.out.println("\nTriangle 5
has sides of :"+tri5);
System.out.println("The perimeter
is "+tri5.calcPerim()+"\nTriangle Properties : ");
if(tri5.isEquilateral())
System.out.println("\t\tEquilateral");
else if(tri5.isIsosceles())
System.out.println("\t\tIsosceles");
else if(tri5.isScalene()) {
System.out.println("\t\tScalene");
}
if(tri5.isRight())
System.out.println("\t\tRight Triangle");
System.out.println();
if(tri2.equals(tri5))
System.out.println("Triangle 2 and Triangle 5 are equal");
else
System.out.println("Triangle 2 and Triangle 5 are not
equal");
if(tri2.isSimilar(tri5))
System.out.println("Triangle 2 and Triangle 5 are similar");
else
System.out.println("Triangle 2 and Triangle 5 are not
similar");
System.out.println("\n The total
perimeter for all "+Triangle.getTriangles()+" is
"+Triangle.getTotalPerimeter());
scan.close();
}
}
// end of Describe.java
Output: