0

我正在练习 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);
    }
}
4

2 回答 2

1

您的 contains 方法(对我来说)似乎是错误的。一方面,您似乎在它们中交换宽度和高度,因为 x 与宽度而不是高度有关,反之亦然。另外,为什么2 * ...在这些方程式中?另外,你确定你在这些方法中的用途吗><在将它们提交给代码之前,您可能希望先在纸上考虑这些方法的逻辑。

接下来,您可能希望格式化您的数字输出以简化结果并摆脱长尾数。

IE,

System.out.printf("Area is %.2f%n", r1.getArea());
于 2013-10-29T04:10:41.060 回答
-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());
}
于 2013-10-29T03:36:52.123 回答