2013-11-21 37 views
0

我想比較應該在使用compareTo方法的主要方法中創建的兩個矩形的面積。通過這樣做,程序應該能夠確定矩形是否相等,或者是否大於或小於矩形。CompareTo - 兩個矩形

我有問題正確使用compareTo方法來確定哪些矩形具有最大面積。

這是代碼已經走了多遠:

Rectangle類:

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; 
    } 
} 
+0

您對BIGGER的定義是什麼?在面積方面? – Fallenreaper

+0

那麼,又是什麼問題? – TigOldBitties

+0

你將不得不嘗試更多一點。要走的路是使用Google搜索compareTo的一些示例,以便了解如何使用它並將其應用於您的問題。 –

回答

0

你聲明你的類實現Comparable<Rectangle>但你的代碼表明你是比較SimpleGeometricObject s。

另外,有什麼可以幫助的是Double類中的函數Double.compare(double d1, double d2)。 (see the documentation