Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MyRectangle2D数学题,检查矩形是否相互包含并重叠_Java_Oop_Object - Fatal编程技术网

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

Java MyRectangle2D数学题,检查矩形是否相互包含并重叠,java,oop,object,Java,Oop,Object,我正在做一个由Y.Daniel Liang编写的Java第九版入门练习。 这个练习是10_13。您必须在其中编程MyRectangle2D类 我已经为这个课程编写了程序,它运行得很顺利,但问题是,当我将程序提交给LiveLab时,我得到了1分。分数为1表示程序运行正常,但您得到的输出不正确 当我提交计划时,我得到的是: 面积为2695亿立方米 周长是20.8 假的 真的 真的 根据LiveLab,这是不正确的 这是我的程序,谁能告诉我哪里出了问题。谢谢 public class Exercise

我正在做一个由Y.Daniel Liang编写的Java第九版入门练习。 这个练习是10_13。您必须在其中编程MyRectangle2D类

我已经为这个课程编写了程序,它运行得很顺利,但问题是,当我将程序提交给LiveLab时,我得到了1分。分数为1表示程序运行正常,但您得到的输出不正确

当我提交计划时,我得到的是:

面积为2695亿立方米

周长是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);
    }
}
尝试:

公共布尔包含(双x,双y)
{
返回2*Math.abs(x-this.x)
您的contains方法(在我看来)似乎有错误。一方面,你们似乎在交换它们的宽度和高度,因为x和宽度相关,而不是和高度相关,反之亦然。还有,为什么这些等式中的
2*…
?另外,您是否确定您的
非常感谢。我接受了你和@aaa的建议,并让它发挥作用。2*…的原因是什么。。。因为我有一种思维定势,认为既然有四个面,它应该是2+x-这个.x等等@user2178846:aaa的建议是错误的。我不是那个投票反对他的人,但无论如何,这是错误的。我用这段代码得到了正确的答案,并通过去掉等式中的“2*Math.abs()”接受了@Hovercraft的全部鳗鱼建议。我最初的解决方案是不使用abs(),但我认为它可能更优雅。现在我意识到我的错误在哪里。它应该是Math.abs(2(x-this.x)-height)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());
}
System.out.printf("Area is %.2f%n", r1.getArea());