質問

I am trying to compare the area of two rectangles that should be created in the main method using the compareTo method. By doing this the program should be able to determain if the rectangles are equal, or if one is bigger or smaller.

I am having problems using the compareTo method correctly to determain which of the rectangles have the biggest area.

This is how far the codes have come:

Rectangle class:

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

        }

}

The 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;
    }
}
役に立ちましたか?

解決

You declare that your class implements Comparable<Rectangle> but your code suggests your are comparing SimpleGeometricObjects.

Also, what could help is the function Double.compare(double d1, double d2) in the Double class. (see the documentation)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top