Just for JAVA Define the Rectangle2D class that contains: Double data fields nam
ID: 3851744 • Letter: J
Question
Just for JAVA
Define the Rectangle2D class that contains: Double data fields named xpos, ypos (that specify the top left comer of the rectangle), width and height. (assume that the rectangle sides are parallel to the x and y axes. See example below). A no-arg constructor that creates a default rectangle with (0.0, 0.0) for (xpos, ypos) and 1.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 getArea () 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 contains (Point p) that returns true if the specified Point (see Exercise 1) has coordinates inside the rectangle. (This is also similar to Figure 1(a).) A method contains (Rectangle2D r) that returns true if the specified rectangle is inside this rectangle. See Figure 1 (b). 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. Write a test program that creates a Rectangle2D object and tests various methods. For example, as a first test case, create a Rectangle2D object r1 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 r1.contains (3, 3) and r1.contains (new Rectangle2D (4, 5, 10.5, 3.2)). Create a Point p using the coordinates 3, 3 and verify that r1.contains (p) returns the same (expected) value.Explanation / Answer
public class Rectangle2D {
/* Data Variables */
private double xpos;
private double ypos;
private double width;
private double height;
/* no arg constructor */
public Rectangle2D() {
this.xpos=0.0;
this.ypos=0.0;
this.width=1.0;
this.height=1.0;
}
/* get methods */
public double getXpos(){
return this.xpos;
}
public double getYpos(){
return this.ypos;
}
public double getWidth(){
return this.width;
}
public double getHeight(){
return this.height;
}
/* set methods */
public void setXpos(double _xpos){
this.xpos=_xpos;
}
public void setYpos(double _ypos){
this.ypos=_ypos;
}
public void setWidth(double _width){
this.width=_width;
}
public void setHeight(double _height){
this.height=_height;
}
/* get area method*/
public double getArea(){
double area=this.width*this.height;
return area;
}
/* get perimeter method*/
public double getPerimeter(){
double perimeter=2*this.width+2*this.height;
return perimeter;
}
/* check if point is inside */
public boolean contains(double x, double y){
double topYCordinate=this.ypos;
double bottomYCordinate=this.ypos-this.height;
double leftXCordinate=this.xpos;
double rightXCordinate=this.xpos+this.width;
/* if point lies inside rectangle return true */
if(x>leftXCordinate && x<rightXCordinate && y>bottomYCordinate && y<topYCordinate)
return true;
return false;
}
/* Check if point is inside
* Uncomment the below code in order to suit with point class
*
**/
/*
public boolean contains(Point p){
return this.contains(p.getX(),p.getY());
}
*/
/* check if rectangle is inside this rectangle */
public boolean contains(Rectangle2D rect){
/* get top left co-ordinates */
double topLeftX=rect.getXpos();
double topLeftY=rect.getYpos();
/* get top right co-ordinates */
double topRightX=rect.getXpos()+rect.getWidth();
double topRightY=rect.getYpos();
/* get bottom left co-ordinates */
double bottomLeftX=rect.getXpos();
double bottomLeftY=rect.getYpos()-rect.getHeight();
/* get bottom right co-ordinates */
double bottomRightX=rect.getXpos()+rect.getWidth();
double bottomRightY=rect.getYpos()-rect.getHeight();
/* All points should be inside this rectangle */
if(this.contains(topLeftX,topLeftY) && this.contains(topRightX,topRightY) && this.contains(bottomLeftX,bottomLeftY) && this.contains(bottomRightX,bottomRightY))
return true;
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.