Question
Define a class named ComparableTriangle that extends Triangle and implements Comparable . Draw the UML diagram and implements the compareTo method to compare the triangles
Define a class named ComparableTriangle that extends Triangle and implements Comparable. Draw the UML diagram and implements the compareTo method to compare the triangles on the basis of area. Write a test class to find the larger of two instances of ComparableTriangle objects. Be sure to accept only valid triangles.
Triangle
public class Triangle extends GeometricObject {
private double side1; private double side2; private double side3;
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { this.side1 = side1; this.side2 = side2; this.side3 = side3; isValidTriangle(); }
public Triangle() { this.side1 = 1; this.side2 = 1; this.side3 = 1; }
public double getArea() {
double s = (side1 + side2 + side3) / 2.0; return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5); }
public double getPerimeter() { return side1 + side2 + side3; }
public String toString() { return "Triangle{" + "side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + '}'; }
public static boolean isTriangle(double side1, double side2, double side3) {
return ((side1 + side2 > side3) && (side1 + side3 > side2) && (side3 + side2 > side1));
}
public double getSide1() { return side1; }
public void setSide1(double side1) throws IllegalTriangleException { this.side1 = side1; isValidTriangle(); }
public double getSide2() { return side2; }
public void setSide2(double side2) throws IllegalTriangleException { this.side2 = side2; isValidTriangle(); }
public double getSide3() { return side3; }
public void setSide3(double side3) throws IllegalTriangleException { this.side3 = side3; isValidTriangle(); }
private void isValidTriangle() throws IllegalTriangleException { if (!isTriangle(side1, side2, side3)) { throw new IllegalTriangleException(side1, side2, side3); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started