2013-10-29 55 views
0

我正在做一個練習,由Y. Daniel Liang介紹給Java 9th Edition。 演習是10_13。你必須編寫MyRectangle2D類的地方。MyRectangle2D數學問題,檢查矩形是否互相包含並重疊

我已經編寫了類,它運行順利,但問題是,當我將程序提交給LiveLab時,我得到的分數爲1.分數1表示程序正確運行,但是您得到輸出不正確。

當我提交的程序,這是我所得到的:

區是26.950000000000003

周長爲20.8

真正

真正

哪個根據麗芙的說法不正確電子實驗室。

這是我的程序,有人可以告訴我我哪裏出錯了。由於

public class Exercise10_13 
{ 
    public static void main(String[] args) 
    { 
    MyRectangle2D r1 = new MyRectangle2D(2, 2, 5.5, 4.9); 
    System.out.println("Area is " + r1.getArea()); 
    System.out.println("Perimeter is " + r1.getPerimeter()); 
    System.out.println(r1.contains(3, 3)); 
    System.out.println(r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2))); 
    System.out.println(r1.overlaps(new MyRectangle2D(3, 5, 2.3, 6.7))); 
    } 
} 
class MyRectangle2D 
{ 
    private double x; 
    private double y; 
    private double height; 
    private double width; 

    public double getX() 
    { 
     return x; 
    } 
    public void setX(double x) 
    { 
     this.x = x; 
    } 
    public double getY() 
    { 
     return y; 
    } 
    public void setY(double y) 
    { 
     this.y = y; 
    } 
    public double getHeight() 
    { 
     return height; 
    } 
    public void setHeight(double height) 
    { 
     this.height = height; 
    } 
    public double getWidth() 
    { 
     return width; 
    } 
    public void setWidth(double width) 
    { 
     this.width = width; 
    } 
    public MyRectangle2D() 
    { 
     this.x = 0; 
     this.y = 0; 
     this.height = 1; 
     this.width = 1; 
    } 
    public MyRectangle2D(double x, double y, double width, double height) 
    { 
     this.x = x; 
     this.y = y; 
     this.width = width; 
     this.height = height; 
    } 
    public double getArea() 
    { 
     return width * height; 
    } 
    public double getPerimeter() 
    { 
     return (width * 2) + (height * 2); 
    } 
    public boolean contains(double x, double y) 
    { 
     return (2 * Math.abs((x-this.x)) > height || 2 * Math.abs((y - this.y)) > width); 
    } 
    public boolean contains(MyRectangle2D r) 
    { 
     return (2 * Math.abs((r.getX()-this.x)) > height || 2 * Math.abs((r.getY() - this.y)) > width); 
    } 
    public boolean overlaps(MyRectangle2D r) 
    { 
     return (2 * Math.abs((r.getX()-this.x)) >= height || 2 * Math.abs((r.getY() - this.y)) >= width); 
    } 
} 

回答

1

你包含的方法出現。(我)是錯誤的。對於你而言,你似乎在交換寬度和高度,因爲x與寬度不相關,而y則相反。另外,爲什麼這些方程中的2 * ...?另外,你確定你的><在這些方法中使用嗎?您可能希望先將這些方法的邏輯先寫在紙上,然後再提交給代碼。

接下來,您可能需要格式化數字輸出以簡化結果並擺脫長尾數。

System.out.printf("Area is %.2f%n", r1.getArea()); 
+0

非常感謝,我接受了你和@aaa的建議並使其工作。 2 * ...是因爲我有這樣一種想法,即從四方開始,它應該是2 + x - th is.x等 – ASmoothNoble

+0

@ user2178846:aaa的建議是錯誤的。我不是那個投他的人,但無論如何,這是錯誤的。 –

-1

嘗試:

public boolean contains(double x, double y) 
{ 
    return 2*Math.abs(x-this.x) < height && 2*Math.abs(y - this.y) < width; 
} 
public boolean contains(MyRectangle2D r) 
{ 
    return contains(r.getX(), r.getY()) && contains(r.getX() + r.getHeight(), r.getY() + r.getWidth()); 
} 
public boolean overlaps(MyRectangle2D r) 
{ 
    return contains(r.getX(), r.getY()) || contains(r.getX() + r.getHeight(), r.getY() + r.getWidth()); 
} 
+0

我通過這段代碼,並採取@Hovercraft充滿鰻魚的建議採取了「2 * Math.abs()的公式所示。 – ASmoothNoble

+0

我原來答對解決方法是不使用abs(),但認爲它可能更優雅,現在我明白我的錯誤在哪裏,它應該是Math.abs(2(x-this.x) - height) aaa