Question

In: Computer Science

In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...

In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity.

Draw the UML diagram for your classes.

Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar triangle. (25 points)

Triangle Similarity: With the Triangle class we have already built, one that uses only side lengths, not angles, in its constructor, there is only one way we can check for similarity: side-side-side similarity. The definition of side-side-side similaritry is:

If all the sides of a triangle are proportional to the corresponding sides of another triangle then the triangles are said to be similar.

in other words: (Triangle1 side1 / Triangle2 side 1) = (Triangle1 side2 / Triangle2 side2) = (Triangle1 side3 / Triangle2 side3)

Solutions

Expert Solution

Java code and screen short of output and Uml diagram shown below


import java.util.*;
class Triangle //super class
{
double side1,side2,side3;
}
class comparableTriangle extends Triangle implements Comparable<comparableTriangle>{
//subclass of Triangle that implement Comparable intefrface
comparableTriangle(double side1,double side2,double side3){//constructor that initialize variable
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
  
public int compareTo(comparableTriangle ct)//return 1 if both triangle are similar
{
if((side1/ct.side1)==(side2/ct.side2)&&(side2/ct.side2)==(side3/ct.side3)) //similarity checking
return 1;
else
return 0;
}
}
public class Main//main class that read user input
{
   public static void main(String[] args) {
  
   double side1,side2,side3;
   Scanner input = new Scanner (System.in); //reading input using Scanner class
   System.out.println("Enter three sides of first triangle:");//reading first triangle
   side1 = input.nextDouble();
   side2 = input.nextDouble();
   side3 = input.nextDouble();
   //first object created and calling constructor with values of sides
   comparableTriangle c1=new comparableTriangle(side1,side2,side3);
   System.out.println("Enter three sides of second triangle:");//reading second triangle
   side1 = input.nextDouble();
   side2 = input.nextDouble();
   side3 = input.nextDouble();
   //second object created and calling constructor with values of sides
comparableTriangle c2=new comparableTriangle(side1,side2,side3);   

if(c2.compareTo(c1)==1)//similarity checking
System.out.println("Triangles are similar");
else
System.out.println("Triangles are not similar");
}
}

output

Enter three sides of first triangle:
14 15 16
Enter three sides of second triangle:
14 15 16
Triangles are similar

Enter three sides of first triangle:
14 18 16
Enter three sides of second triangle:
14 15 16
Triangles are not similar


screen short of ouput

Uml diagram shown below


Related Solutions

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...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method:...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method: /** Return the height of this binary tree */ public int height() Class Name: Exercise25_01
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT