Sections 14.1-14.7: Comparable) Modify The
Sections 14.1-14.7: Comparable) Modify The
Sections 14.1-14.7: Comparable) Modify The
7
14.1* (Enabling GeometricObject comparable) Modify the GeometricObject
class to implement the Comparable interface, and define a static max method in
the GeometricObject class for finding the larger of two GeometricObject
objects. Draw the UML diagram and implement the new GeometricObject
class. Write a test program that uses the max method to find the larger of two circles
and the larger of two rectangles.
package
exercise1
;
/** SimpleGoemetricObject UML
* -------------------------------------
* -color: String +s +g
* -filled: boolean +s +g
* -dateCreated: Date +g
* -------------------------------------
* +SimpleGeoMetricObject()
* +SimpleGoometricObject(color: String, filled: boolean)
* +toString(): String
* -------------------------------------
*
* @author Amadeusz
*
*/
public abstract class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
package
Chapter_13.Chapter_Classes
;
import Chapter_10.Chapter_Classes.Circle2D;
public class ComparableCircle extends Circle2D implements
Comparable<ComparableCircle> {
// default no-arg constructor invoked from superclass
public ComparableCircle(){
}
@Override
public int compareTo(ComparableCircle o) {
if (getArea() > o.getArea()){
return 1;
} else if(getArea() < o.getArea()){
return -1;
} else {
return 0;
}
}
14.3* (The Colorable interface) Design an interface named Colorable with a void
method named howToColor(). Every class of a colorable object must implement
the Colorable interface. Design a class named Square that extends
GeometricObject and implements Colorable. Implement howToColor to
display a message "Color all four sides".
Draw a UML diagram that involves Colorable, Square, and GeometricObject.
Write a test program that creates an array of five GeometricObjects.
For each object in the array, invoke its howToColor method if it is
colorable.
14.4* (Revising the House class) Rewrite the House class in Listing 14.9 to perform a
deep copy on the whenBuilt field.
14.5* (Enabling Circle comparable) Rewrite the Circle class in Listing 14.2 to extend
GeometricObject and implement the Comparable interface. Override the
equals method in the Object class. Two Circle objects are equal if their radii
are the same. Draw the UML diagram that involves Circle, GeometricObject,
and Comparable.
14.6* (Enabling Rectangle comparable) Rewrite the Rectangle class in Listing 14.3
to extend GeometricObject and implement the Comparable interface. Override
the equals method in the Object class. Two Rectangle objects are equal
if their areas are the same. Draw the UML diagram that involves Rectangle,
GeometricObject, and Comparable.
14.7* (The Octagon class) Write a class named Octagon that extends GeometricObject
and implements the Comparable and Cloneable interfaces. Assume that all eight
sides of the octagon are of equal size. The area can be computed using the following
formula:
Draw the UML diagram that involves Octagon, GeometricObject,
Comparable, and Cloneable. Write a test program that creates an Octagon
object with side value 5 and displays its area and perimeter. Create a new
object using the clone method and compare the two objects using the
compareTo method.