Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Create a program in Java with the following requirements: Create a Java class named MyRectangle2D.java. Your class will have double two variables named x and

Create a program in Java with the following requirements:

  • 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 the name "John Doe".
  • 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 the program, my professor will create a class with a main method that creates instances of the 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions