Tuesday, 20 December 2016

Chapter 13 Exercise 10, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

13.10 (Enable MyRec2 comparable)
Rewrite the MyRec2 class in Listing 13.3 to extend GeometricObject
and implement the Comparable interface. Override the equals method in
the Object class. Two MyRec2 objects are equal if their areas are the same.
Draw the UML diagram that involves MyRec2, GeometricObject, and Comparable.

public abstract class GeometricObject implements Comparable<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();
    }

    /** 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,
     *  the get 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 */
    @Override
    public String toString() {
        return "created on " + dateCreated + "\ncolor: " + color +
                " and filled: " + filled;
    }

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

    public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
        return (o1.compareTo(o2) >= 0) ? o1 : o2;
    }

    public static double sumArea(GeometricObject[] a) {
        double sum = 0;
        for (GeometricObject o : a) {
            sum += o.getArea();
        }
        return sum;
    }
    /** Abstract method getArea */
    public abstract double getArea();

    /** Abstract method getPerimeter */
    public abstract double getPerimeter();
}

public class Exercise_10 {

    public static void main(String[] args) {

        Rectangle rec1 = new Rectangle(10,10);
        Rectangle rec2 = new Rectangle(10, 10);
        System.out.println(rec1.equals(rec2));
        System.out.println(rec1.compareTo(rec2));
    }

    private static class Rectangle extends GeometricObject {
        private double width;
        private double height;

        public Rectangle() {
        }

        public Rectangle(
                double width, double height) {
            this.width = width;
            this.height = height;
        }

        public Rectangle(
                double width, double height, String color, boolean filled) {
            this.width = width;
            this.height = height;
            setColor(color);
            setFilled(filled);
        }

        /** Return width */
        public double getWidth() {
            return width;
        }

        /** Set a new width */
        public void setWidth(double width) {
            this.width = width;
        }

        /** Return height */
        public double getHeight() {
            return height;
        }

        /** Set a new height */
        public void setHeight(double height) {
            this.height = height;
        }

        /** Return area */
        public double getArea() {
            return width * height;
        }

        /** Return perimeter */
        public double getPerimeter() {
            return 2 * (width + height);
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof Rectangle && getArea() == ((Rectangle) o).getArea();
        }
    }
}

No comments :

Post a Comment