Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Geometry: the MyRectangle2D class) Define the MyRectangle2D class that contains

ID: 3556033 • 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 get and set methods. (Assume that the rectangle sides are parallel to xor
y- axes.)
? The data fields width and height with get and set 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.18a).
? A method contains(MyRectangle2D r) that returns true if the specified
rectangle is inside this rectangle (see Figure 10.18b).
? A method overlaps(MyRectangle2D r) that returns true if the specified
rectangle overlaps with this rectangle (see Figure 10.18c).

For the CS Rectangle 2D programs, an initial rectangle should be entered by the user followed by test points and other rectangles. The program should be structured so that the use enters the first rectangle which should not have to be entered again. Every time the program is run with new test points and rectangles the new shapes should be compared to the same rectangles entered first. There should be two input loops, the first which starts the program again at the beginning and allows the basic rectangle to be entered, and within it a loop which allows entering multiple test points and rectangles.

Explanation / Answer

public class Solution
{
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);
    }
}