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.
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
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 function in JAVA called shorten that is defined in the class P8 and...
. Write a function in JAVA called shorten that is defined in the class P8 and shortens each element of an array of strings. Every string with more than two characters is cut down to its first two characters. For example, a program that uses the function shorten follows. public class P8 { public static void main(String args[]) { String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"}; shorten(x); for (int i = 0; i < 6; i++) System.out.print(x[i] +...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT