Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm confused. Help me out!!! Define a class named ComparableTriangle that extends Triangle and implements Comparable . Draw the UML diagram and implements the compareTo

I'm confused. Help me out!!!

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.

These are what I made so far.

public abstract class GeometricObject {

private String color = "white"; private boolean filled; private java.util.Date dateCreated;

//construct for a geometric object public GeometricObject() { dateCreated = new java.util.Date(); }

//constructor with color public GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }

//return color public String getColor() { return color; }

//return color public void setColor(String color) { this.color = color; }

//return filled public boolean isFilled() { return filled; }

//set a new filled public void setFilled(boolean filled) { this.filled = filled; }

//get dateCreated public java.util.Date getDateCreated() { return dateCreated; }

//return to a string public String toString() { return "created on " + dateCreated + " color: " + color + " and filled: " + filled; }

}

@@@

public class IllegalTriangleException extends IllegalArgumentException {

private double s1; private double s2; private double s3;

//super class constructor with arguments public IllegalTriangleException(double s1, double s2, double s3) { super("Not a real triangle:" + " side1 = " + s1 + " side2 = " + s2 + " side3 = " + s3); this.s1 = s1; this.s2 = s2; this.s3 = s3; }

//return s1 public double getS1() { return s1; }

//return s2 public double getS2() { return s2; }

//return s2 public double getS3() { return s3; }

}

@@@

public class Triangle extends GeometricObject {

private double side1; private double side2; private double side3;

//constructor with arguments public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { this.side1 = side1; this.side2 = side2; this.side3 = side3; isValidTriangle(); }

//constructor for initial condition public Triangle() { this.side1 = 1; this.side2 = 1; this.side3 = 1; }

//override public double getArea() {

double s = (side1 + side2 + side3) / 2.0; return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5); }

//override public double getPerimeter() { return side1 + side2 + side3; }

//override public String toString() { return "Triangle{" + "side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + '}'; } //requiremenst for triangle

public static boolean isTriangle(double side1, double side2, double side3) {

return ((side1 + side2 > side3) && (side1 + side3 > side2) && (side3 + side2 > side1));

} //return side1

public double getSide1() { return side1; } //check

public void setSide1(double side1) throws IllegalTriangleException { this.side1 = side1; isValidTriangle(); } //return side2

public double getSide2() { return side2; } //check

public void setSide2(double side2) throws IllegalTriangleException { this.side2 = side2; isValidTriangle(); } //return side3

public double getSide3() { return side3; } //check

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