Interfaces
Comparable
The program uses the Comparable interface and the max method to find a maximum circle between two circles and a maximum cylinder between two cylinders.
// TestInterface.java: Use the Comparable interface // and the generic max method to find max objects public class TestInterface { /** Main method */ public static void main(String[] args) { // Create two comparable circles ComparableCircle circle1 = new ComparableCircle(5); ComparableCircle circle2 = new ComparableCircle(4); // Display the max circle Object circle = Max.max(circle1, circle2); System.out.println("The max circle's radius is " + ((Circle)circle).getRadius()); System.out.println(circle); } } // ComparableCircle is a subclass of Circle, which implements the // Comparable interface class ComparableCircle extends Circle implements Comparable { /** Construct a ComparableCircle with a specified radius */ public ComparableCircle(double r) { super(r); } /** Implement the compareTo method defined in Comparable */ public int compareTo(Object o) { if (getRadius() > ((Circle)o).getRadius()) return 1; else if (getRadius() < ((Circle)o).getRadius()) return -1; else return 0; } }
// Max.java: Find a maximum object public class Max { /** Return the maximum between two objects */ public static Object max(Object o1, Object o2) { if (((Comparable)o1).compareTo(o2) > 0) return o1; else return o2; } }
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** 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(); }