Question: Hello, I'm trying to make java program that has a class called MyRectangle2D. This class is suppose to make a rectangle using point(x,y) and width

Hello,

I'm trying to make java program that has a class called MyRectangle2D. This class is suppose to make a rectangle using point(x,y) and width and height. Also, it

has some properties and methods.

In main function, I need to make two objects. Each object has point(x,y), width and height. Then I need to determine if the second rectangle is:

1- inside the first one or not

2- overlaps with the first one

3- is abut

4- is distinct from the first rectangle

my question is:

1- is the code provided correct? below I've posted a code for points 1 and 2

2- can I get some help with point 3 and 4? I really don't know the math to do that.

THANKS in advance.

Code for point 1 and 2:

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!