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

Java Program The MyRectangle2D class was defined in Exercise 10.12. Write an app

ID: 647882 • Letter: J

Question

Java Program

The MyRectangle2D class was defined in Exercise 10.12. Write an application that enables the user to specify the location and size of the rectangles and displays whether the two rectangles intersect, as shown in the following figure. Enable the user to point the mouse inside a rectangle and drag it. As the rectangle being dragged, the rectangle center coordinates in the text fields are updated.

Java Program The MyRectangle2D class was defined in Exercise 10.12. Write an application that enables the user to specify the location and size of the rectangles and displays whether the two rectangles intersect, as shown in the following figure. Enable the user to point the mouse inside a rectangle and drag it. As the rectangle being dragged, the rectangle center coordinates in the text fields are updated.

Explanation / Answer

Program:

public class Excercise10.12 {

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);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote