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...
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++)          {...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT