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

Need to be answered correctly. You must: Comment and Format and Must make sure a

ID: 3880567 • Letter: N

Question

Need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding as (.......).java and (........)Demo.java (Public class must be based on the question for both (.......).java and (........)Demo.java ) DON'T FORGET ABOUT COMMENTING AND FOLLOW EXACTLY WHAT HAS BEEN ASKED IN THE QUESTION.

Need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding as (.......).java and (........)Demo.java (Public class must be based on the question for both (.......).java and (........)Demo.java ) DON'T FORGET ABOUT COMMENTING AND FOLLOW EXACTLY WHAT HAS BEEN ASKED IN THE QUESTION.

Exercise 1: Define the Rectangle2D class that contains Double data fields named xpos ypos (that speci width and height, (assume that the rectangle sides are parallel to the x and y axes. See example below). e top left corner of the rectangle), (0.0,0.0) 10.0, 5.0) height 2.0 width- 3.0 . A no-arg constructor that creates a default rectangle with (0.0, 0.0) for (xpos. ypos) and 0.0 for both width and height A constructor that creates a rectangle with the specified xpos, ypos, width, and height Get and set methods for all the instance variables . . A method getAreal that returns the area of the rectangle. . A method getPerimeter that returns the perimeter of the rectangle. . A method contains(x_y) that returns true if the specified point (x. y) is inside this rectangle. See Figure 1(a). A method rectangle. See Figure lb). Note that the condition for contains is that all four corners of the second rectangle must be contained in the first. This means, that if the two rectangles are exactly identical, we still say that the second rectangle is contained within the other. that returns true if the specified rectangle is inside this Figure 1 For example, Write a test program that creates a Rectangle2D object and tests various methods. as a first test case, create a Rectangle2D object rl with parameters 2, 2, 5.5 and 4.9, which represent xpos, ypos, width and height, respectively, displays its area and perimeter, and displays the result of rLcontains(3, 3) and rl.contains(new Rectangle2D(4, 5, 10.5, 3.2)) Try it for two other cases.

Explanation / Answer

public class Rectangle2D
{
private double xpos,ypos,width,height;

public Rectangle2D() // default constructor
{
xpos = 0;
ypos = 0;
width = 0;
height = 0;
}
public Rectangle2D(double xpos,double ypos,double width,double height) //argument constructor
{
this.xpos = xpos;
this.ypos = ypos;
this.width = width;
this.height = height;
}
//get and set methods
public double getX()
{
return xpos;
}
public void setX(double x)
{
this.xpos = xpos;
}

public double getY()
{
return ypos;
}
public void setY(double y)
{
this.ypos = ypos;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return height;
}

public double getPerimeter()
{
return 2*(width + height);
}
public double getArea()
{
return width*height;
}
public boolean contains(double xpos,double ypos)
{
if(this.xpos <= xpos && this.width > xpos && this.ypos <= ypos && this.height > ypos) // this.x,this.y is starting point of rectangle
return true;
else
return false;
}
public boolean contains(Rectangle2D r)
{
if(this.xpos <= r.getX() && this.width > r.getX() && this.ypos <= r.getY() && this.height > r.getY()) // this.x,this.y is starting point of rectangle
return true;
else
return false;
}
public boolean overlaps(Rectangle2D r)
{
if((this.xpos+this.width) >= r.getX()&& (this.xpos+this.width) <= (r.getX()+r.getWidth()) && (this.ypos+this.height) >= r.getY() && (this.ypos+this.height) <= r.getY()+r.getHeight() ) // this.x,this.y is starting point of rectangle
return true;
else
return false;
}
}
public class Demo
{
public static void main (String[] args)
{
Rectangle2D rect = new Rectangle2D(3.6,4.1,7.2,8.5);
Rectangle2D rect1 = new Rectangle2D(3.7,4.5,6.6,7.9);

Rectangle2D rect2 = new Rectangle2D(1.7,2.3,6.1,5.7);

System.out.println("rect contains (4.5,15.8) : "+rect.contains(4.5,15.8));
System.out.println("rect contains rect1 : "+rect.contains(rect1));
System.out.println("rect2 overlaps rect : "+rect2.overlaps(rect));

}
}

output:

rect contains (4.5,15.8) : false
rect contains rect1 : true
rect2 overlaps rect : true

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