我正在做一個練習,由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);
}
}
非常感謝,我接受了你和@aaa的建議並使其工作。 2 * ...是因爲我有這樣一種想法,即從四方開始,它應該是2 + x - th is.x等 – ASmoothNoble
@ user2178846:aaa的建議是錯誤的。我不是那個投他的人,但無論如何,這是錯誤的。 –