(Geometry: the MyRectangle2D class) Define the MyRectangle2D class that contains
ID: 3562388 • Letter: #
Question
(Geometry: the MyRectangle2D class) Define the MyRectangle2D class that
contains:
? Two double data fields named x and y that specify the center of the rectangle
with getter and setter methods. (Assume that the rectangle sides are
parallel to x- or y- axes.)
? The data fields width and height with getter and setter methods.
? A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and
1 for both width and height.
? A constructor that creates a rectangle with the specified x, y, width, and
height.
? A method getArea() that returns the area of the rectangle.
? A method getPerimeter() that returns the perimeter of the rectangle.
? A method contains(double x, double y) that returns true if the
specified point (x, y) is inside this rectangle (see Figure 10.24a).
? A method contains(MyRectangle2D r) that returns true if the specified
rectangle is inside this rectangle (see Figure 10.24b).
? A method overlaps(MyRectangle2D r) that returns true if the specified
rectangle overlaps with this rectangle (see Figure 10.24c).
Explanation / Answer
public class MyRectangle2D {
private double x;
private double y;
private double width;
private double height;
public MyRectangle2D() {
this.x = 0.0;
this.y = 0.0;
this.width = 0.0;
this.height = 0.0;
}
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 2 * (width + height);
}
// A method contains(MyRectangle2D r) that returns true if the specified
// rectangle is inside this rectangle
public boolean contains(MyRectangle2D point) {
return ((point.x + point.width / 2 <= this.x + width / 2)
&& (point.x - point.width / 2 >= this.x - width / 2)
&& (point.y - point.height / 2 >= this.y - height / 2) && (point.y
+ point.height / 2 <= this.y + height / 2));
}
public boolean contains(double x, double y) {
return ((x <= this.x + width / 2) && (x >= this.x - width / 2)
&& (y >= this.y - height / 2) && (y <= this.y + height / 2));
}
// A method overlaps(MyRectangle2D r) that returns true if the specified
// rectangle overlaps with this rectangle
public boolean overlaps(MyRectangle2D other) {
// the other rectangle is said to overlap if any one of the following
// condition evaluates to true;
// top right edge is within the rectangle
boolean overlaps = contains(other.x - width / 2, other.y - height / 2);
// OR
// bottom left edge is with in the rectangle
overlaps = overlaps
|| contains(other.x + width / 2, other.y + height / 2);
return overlaps;
}
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 getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.