0% found this document useful (0 votes)
91 views

Sections 14.1-14.7: Comparable) Modify The

The document describes exercises to modify and extend existing geometric shape classes to implement the Comparable interface and enable comparison of objects. It involves: 1. Modifying the GeometricObject class to implement Comparable and add a max method to find the larger of two objects. 2. Creating a ComparableCircle class that extends Circle and implements Comparable to compare circles based on area. 3. Designing a Colorable interface and Square class to demonstrate interfaces.

Uploaded by

Irene Sultana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Sections 14.1-14.7: Comparable) Modify The

The document describes exercises to modify and extend existing geometric shape classes to implement the Comparable interface and enable comparison of objects. It involves: 1. Modifying the GeometricObject class to implement Comparable and add a max method to find the larger of two objects. 2. Creating a ComparableCircle class that extends Circle and implements Comparable to compare circles based on area. 3. Designing a Colorable interface and Square class to demonstrate interfaces.

Uploaded by

Irene Sultana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sections 14.1–14.

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;

/** Construct a default geometric object */


public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public SimpleGeometricObject(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,


its getter method is named 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;
}
}
14.2* (The ComparableCircle class) Create a class named ComparableCircle that
extends Circle and implements Comparable. Draw the UML diagram and
implement the compareTo method to compare the circles on the basis of area.
Write a test class to find the larger of two instances of ComparableCircle
objects.

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(){
}

// args constructor invoked from superclass


public ComparableCircle(double x, double y, double
radius){
super(x, y, radius);
}

@Override
public int compareTo(ComparableCircle o) {
if (getArea() > o.getArea()){
return 1;
} else if(getArea() < o.getArea()){
return -1;
} else {
return 0;
}
}

/** Overrided method allows to check if two objects


* of ComparableCircle class are equals */
@Override
public boolean equals(Object obj) {
return (obj instanceof ComparableCircle &&
this.getRadius() == ((ComparableCircle) obj).getRadius());
}
/** Returns max from 2 objects using compareTo method
(Comparable interface) */
public static ComparableCircle max(ComparableCircle c1,
ComparableCircle c2){
if (c1.compareTo(c2) >= 0){
return c1;
} else{
return c2;
}
}
}

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.

You might also like