MyRectangle2D.java
public class MyRectangle2D {
//declare variables that represent center point of the
rectangle
private double x;
private double y;
//declare variables that represent the width and
height of the rectangle
private double width;
private double height;
//getters
//method that returns x coordinates of the center
point of the rectangle
public double getX()
{
return this.x;
}
//method that returns y coordinate of the center
point of the rectangle
public double getY()
{
return this.y;
}
//method that returns the width of the
rectangle
public double getWidth()
{
return this.width;
}
//method that returns the height of the
rectangle
public double getHeight()
{
return this.height;
}
//setters
//method that sets x coordinates of the center point
of the rectangle
public void setX(double x)
{
this.x = x;
}
//method that sets y coordinate of the center point
of the rectangle
public void setY(double y)
{
this.y = y;
}
//method that sets the width of the rectangle
public void setWidth(double width)
{
this.width = width;
}
//method that sets the height of the
rectangle
public void setHeight(double height)
{
this.height = height;
}
//no argument constructor
public MyRectangle2D()
{
this.x = 0;
this.y = 0;
this.width = 1;
this.height = 1;
//display your first and last name
(replace the below with your first and last name)
System.out.println("First name:
FirstName\t Last name : LastName");
}
//constructor with parameters
public MyRectangle2D(double x, double y, double width,
double height)
{
//If the x or y input parameters
are less than 0.0, set them to be 0.0
if(x < 0 || y < 0)
{
this.x =
0.0;
this.y =
0.0;
}
else
{
this.x =
x;
this.y =
y;
}
//If the width or height input
parameters are less than 1.0, set them to be 1.0
if(width < 1.0 || height <
1.0)
{
this.width =
1.0;
this.height =
1.0;
}
else
{
this.width =
width;
this.height =
height;
}
}
//method myTop that returns the y coordinate of the
top of the rectangle
public double myTop()
{
return this.y + (0.5 *
this.height);
}
//method myBottom that returns the y coordinate of
the bottom of the rectangle
public double myBottom()
{
return this.y - (0.5 *
this.height);
}
//method myLeft that returns the x coordinate of
the left side of the rectangle
public double myLeft()
{
return this.x - (0.5 *
width);
}
//method myRight that returns the x coordinate of
the left side of the rectangle
public double myRight()
{
return this.x + (0.5 *
this.width);
}
//method getArea that returns the area of the
rectangle
public double getArea()
{
return this.width *
this.height;
}
//method getPerimeter that returns the perimeter of
the rectangle
public double getPerimeter()
{
return (2 * this.width) + (2 *
this.height);
}
//method contains(double inX, double inY) that
returns true if the point (inX,inY) is inside the rectangle and
false otherwise.
public boolean contains(double x, double y)
{
if(((x - this.x) <=
this.width/2) && ((y - this.y) <= this.height/2))
return
true;
else
return
false;
}
}
TestMyRectangle2D.java
public class TestMyRestangle2D {
public static void main(String[] args) {
//create an object to MyRectangle2D
class
MyRectangle2D myRectangle = new
MyRectangle2D(2.0, 1.0, 1.0, 2.0);
//getX would return 2.0
System.out.println("x: " +
myRectangle.getX());
//getY would return 1.0
System.out.println("y: " +
myRectangle.getY());
//getWidth would return 1.0
System.out.println("width: " +
myRectangle.getWidth());
//getHeight would return 2.0
System.out.println("height: " +
myRectangle.getHeight());
//myTop would return 2.0
System.out.println("myTop: " +
myRectangle.myTop());
//myBottom would return 0.0
System.out.println("myBottom: " +
myRectangle.myBottom());
//myLeft would return 1.5
System.out.println("myLeft: " +
myRectangle.myLeft());
//myRight would return 2.5
System.out.println("myRight: " +
myRectangle.myRight());
//getArea would return 2.0
System.out.println("area: " +
myRectangle.getArea());
//getPerimeter would return
6.0
System.out.println("perimeter: " +
myRectangle.getPerimeter());
//contains(1.75,0.9) would return
true
System.out.println("contain(1.75,
0.9): " + myRectangle.contains(1.75, 0.9));
//contains(2.5,2.25) would return
false
System.out.println("contain(2.5,
2.25): " + myRectangle.contains(2.5, 2.25));
}
}
Output:

Program screenshot:






Requirements: programming language is java Create a Java class named “MyRectangle2D.java”. Your class will have double...