Question

In: Computer Science

Java Write a class called Triangle that can be used to represent a triangle. Write a...

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.

Solutions

Expert Solution

// 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:



Related Solutions

Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Write a Java application, and an additional class to represent some real-world entity such as a...
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might...
java create a class for triangle and also driver(area and perimeter) also write its getters and...
java create a class for triangle and also driver(area and perimeter) also write its getters and setters for the right triangle
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT