Problem Description
Design a new Triangle class that extends the abstract GeometricObject class (listing 13.1, section 13.2). Draw the UML diagram for the classes Triangle and GeometricObject, and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.
GeometricObject:
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
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 a string representation of this object */
public String toString() {
return "created on " + dateCreated + "ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
Example of a comparator:
import java.util.Comparator;
public class GeometricObjectComparator
implements Comparator, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
double area1 = o1.getArea();
double area2 = o2.getArea();
if (area1 < area2)
return -1;
else if (area1 == area2)
return 0;
else
return 1;
}
}
Sample run
Enter three sides of a triangle: 3 4 5
Enter a color: blue
Enter a Boolean value to indicate if the triangle is filled: false
Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0
Area: 6.00
Perimeter: 12.00
Color: blue
Filled: false
Problem Description:
Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses named Circle and Rectangle. Write a test program that uses the max method to find the larger of two circles (c1 of the radius 1.2 and c2 of the radius 1.2) and the larger of one circle (c1 of the radius 1.2) and one rectangle (of the length 5 and width 3.4.
Sample run
Comparing two circles:
The two shapes are equal.
Comparing a circle and a rectangle:
The greater shape is the rectangle of length 5.0 and width 3.4