Question

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

Requirements:
programming language is java

Create a Java class named “MyRectangle2D.java”.
Your class will have double two variables named x and y. These will represent the center point of your rectangle.
Your class will have two double variables named width and height. These will represent the width and height of your rectangle.
Create getter and setter methods for x, y, width, and height.
Create a “no argument” constructor for your class that sets x to 0, y to 0, width to 1, and height to 1. Have this constructor output your first and last name.
Create a constructor for your class that has x, y, width, and height input parameters.
If the x or y input parameters are less than 0.0, set them to be 0.0
If the width or height input parameters are less than 1.0, set them to be 1.0
Create method myTop that returns the y coordinate of the top of the rectangle
This would be y + 0.5*height.
Create method myBottom that returns the y coordinate of the bottom of the rectangle
This would be y – 0.5*height.
Create method myLeft that returns the x coordinate of the left side of the rectangle
This would be x – 0.5*width.
Create method myRight that returns the x coordinate of the left side of the rectangle
This would be x + 0.5*width.
Create method getArea that returns the area of the rectangle
The area is the width * height
Create method getPerimeter that returns the perimeter of the rectangle
The perimeter is 2 * width + 2 * height
Create method contains(double inX, double inY) that returns true if the point (inX,inY) is inside the rectangle and false otherwise.
To test your program, I will create a class with a main method that creates instances of your MyRectangle2D class, calling all the constructors and method.
For example, assume a rectangle was created with x=2.0, y=1.0, width=1.0, and height=2.0
getX would return 2.0
getY would return 1.0
getWidth would return 1.0
getHeight would return 2.0
myTop would return 2.0
myBottom would return 0.0
myLeft would return 1.5
myRight would return 2.5
getArea would return 2.0
getPerimeter would return 6.0
contains(1.75,0.9) would return true
contains(2.5,2.25) would return false
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Requirements: programming language is java Create a Java class named “MyRectangle2D.java”. Your class will have double...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT