我正在做一个关于在物体中思考的练习。我完成了编写没有错误的代码,但是当我运行它时,我得到的输出不正确。 这就是我得到的:
面积:26.950000000000003
周长:20.8
包含:false
包含:true
包含:true
我做错了什么?有人可以告诉我哪里出错了吗?! - 我被困在这里 -
有我的代码:
public class Excercise10_13_1 {
public static void main(String[] args) {
MyRectangle2D r1 = new MyRectangle2D(2, 2, 5.5, 4.9);
System.out.println("area: " + r1.getArea());
System.out.println("perimeter: " + r1.getPerimeter());
System.out.println("contains: " + r1.contains(3, 3));
System.out.println("contains: "
+ r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)));
System.out.println("contains: "
+ r1.overlaps(new MyRectangle2D(3, 5, 2.3, 5.4)));
}
}
class MyRectangle2D {
private double x, y;
private double width, height;
MyRectangle2D() {
x = 0;
y = 0;
width = 1;
height = 1;
}
MyRectangle2D(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public boolean contains(double x, double y) {
return (x >= this.x && x <= (this.x + width) && y >= this.y && y <= (this.y + height)) ? false
: true;
}
public boolean contains(MyRectangle2D r) {
return Math.abs(2 * (r.getX() - this.x) - height) > height
|| Math.abs(2 * (r.getY() - this.y)) > width;
}
public boolean overlaps(MyRectangle2D r) {
return (Math.abs(2 * (r.getX() - this.x)) >= height || Math.abs(2 * (r
.getY() - this.y)) >= width);
}
}