CompareTo - 两个矩形

时间:2013-11-21 19:08:50

标签: java rectangles compareto

我试图比较应该使用compareTo方法在main方法中创建的两个矩形的区域。通过这样做,程序应该能够阻止矩形是否相等,或者是否更大或更小。

我无法正确使用compareTo方法来阻止哪个矩形具有最大的区域。

这是代码的范围:

矩形类:

public abstract class Rectangle extends SimpleGeometricObject implements Comparable <Rectangle>{
    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;
    }

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

    public boolean equals(Object obj) {
        return this.getArea() == ((Rectangle)obj).getArea();
    }

        public int compareTo(SimpleGeometricObject o) {
            if (this.getArea() > ((Rectangle) o).getArea()) {
                return 1;
            } else if (this.getArea() < ((Rectangle) o).getArea()) {
                return -1;
            } else {
                return 0;
            }
        }




        public static void main(String[] args) {

        }

}

SimpleGeometricObject:

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

1 个答案:

答案 0 :(得分:0)

您声明您的类实现了Comparable<Rectangle>,但您的代码建议您正在比较SimpleGeometricObject s。

此外,Double.compare(double d1, double d2)类中的函数Double可能有所帮助。 (see the documentation