MyRectangle2D数学问题,检查矩形是否相互包含并重叠

时间:2013-10-29 03:24:44

标签: java oop object

我正在使用Y. Daniel Liang的Java 9th Edition简介进行练习。 练习是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);
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

即,

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

答案 1 :(得分:-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());
}