Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How do XHTML, CSS, and XML work together to create a Web page?

Answered: 1 week ago

Question

19. Are your employees performing below their capabilities?

Answered: 1 week ago