我正在练习 Y. Daniel Liang 的 Java 9 版简介。练习是 10_13。您必须对 MyRectangle2D 类进行编程。
我已经对课程进行了编程,它运行顺利,但问题是,当我将程序提交到 LiveLab 时,我得到 1 分。1 分意味着程序运行正确,但你得到的输出不正确。
当我提交程序时,这就是我得到的:
面积为 26.950000000000003
周长为 20.8
错误的
真的
真的
根据 LiveLab,这是不正确的。
这是我的程序,有人可以告诉我哪里出错了。谢谢
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);
}
}