Question

please do in java and comments the code so i can understand Requirements: Create a Java...

please do in java and comments the code so i can understand

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 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

//Java code

public class MyRectangle2D {
/**
 * double two variables named x and y.
 * These will represent the center point of your rectangle.
 */
private double x;
private double y;
/**
 * two double variables named width and height.
 * These will represent the width and height of your rectangle.
 */
private  double width;
private double height;
/***
 * getter and setter methods for x, y, width, and height.
 */
    public double getX() {
        return x;
    }

    public void setX(double x) {
        if(x<0)
            x=0;
        this.x = x;
    }

    public double getY() {

        return y;
    }

    public void setY(double y) {
        if(y<0)
            y=0;
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        if(width<1)
            width=1;
        this.width = width;
    }

    public double getHeight() {

        return height;
    }

    public void setHeight(double height) {
        if(width<1)
            width=1;
        this.height = height;
    }
    /**
     * 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.
     */
    public MyRectangle2D()
    {
        x=0;
        y=0;
        height=1;
        width=1;
    }
    /**
     *  a constructor for your class that has x, y,
     *  width, and height input parameters.
     */
    public MyRectangle2D(double x, double y, double width, double height) {
      setX(x);
      setY(y);
      setHeight(height);
      setWidth(width);
    }
    /**
     *  myTop that returns the y coordinate of the top of the rectangle
     * This would be y + 0.5*height.
     */
    public double myTop()
    {
        return  y + 0.5*height;
    }
    /**
     *  method myBottom that returns the y coordinate
     *  of the bottom of the rectangle
     * This would be y – 0.5*height.
     */
    public double myBottom()
    {
        return y-0.5*height;
    }
    /**
     *  method myLeft that returns the x
     *  coordinate of the left side of the rectangle
     * This would be x – 0.5*width.
     */
    public double myLeft()
    {
        return x - 0.5*width;
    }
    /**
     *  method myRight that returns the x
     *coordinate of the left side of the rectangle
     * This would be x + 0.5*width.
     */
    public double myRight()
    {
        return  x + 0.5*width;
    }
    /**
     * method getArea that returns the area of the rectangle
     * The area is the width * height
     */
    public double getArea()
    {
        return height*width;
    }
    /**
     *  method getPerimeter that returns the perimeter of the rectangle
     * The perimeter is 2 * width + 2 * height
     */
    public double getPerimeter()
    {
        return 2*(width+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 inX, double inY)
    {
        return (
                x>inX && y >inY &&
              x + width >inX  &&
              y + height > inY);
    }
}

//============================

public class Main {
    public static void main(String[] args)
    {
        /**
         * creates instances of your MyRectangle2D class,
         * calling all the constructors and method.
         */
        /**
         *  a rectangle was created with x=2.0, y=1.0, width=1.0, and height=2.0
         */
        MyRectangle2D rectangle2D = new MyRectangle2D(2.0,1.0,1.0,2.0);
        //getX would return 2.0
        System.out.println("getX would return 2.0");
        System.out.println(rectangle2D.getX());
        //getY would return 1.0
        System.out.println("getY would return 1.0");
        System.out.println(rectangle2D.getY());
        //getWidth would return 1.0
        System.out.println("getWidth would return 1.0");
        System.out.println(rectangle2D.getWidth());
        //getHeight would return 2.0
        System.out.println("getHeight would return 2.0");
        System.out.println(rectangle2D.getHeight());
        //myTop would return 2.0
        System.out.println("myTop would return 2.0");
        System.out.println(rectangle2D.myTop());
        //myBottom would return 0.0
        System.out.println("myBottom would return 0.0");
        System.out.println(rectangle2D.myBottom());
        //myLeft would return 1.5
        System.out.println("myLeft would return 1.5");
        System.out.println(rectangle2D.myLeft());
        //myRight would return 2.5
        System.out.println("myRight would return 2.5");
        System.out.println(rectangle2D.myRight());
        //getArea would return 2.0
        System.out.println("getArea would return 2.0");
        System.out.println(rectangle2D.getArea());
        //getPerimeter would return 6.0
        System.out.println("getPerimeter would return 6.0");
        System.out.println(rectangle2D.getPerimeter());
        //contains(1.75,0.9) would return true
        System.out.println("contains(1.75,0.9) would return true");
        System.out.println(rectangle2D.contains(1.75,0.9));
        //contains(2.5,2.25) would return false
        System.out.println("contains(2.5,2.25) would return false");
        System.out.println(rectangle2D.contains(2.5,2.25));
    }
}

//Output

C:\Program Files\Java\jdk1.8.0_221\bin\java.exe ... getX would return 2.0 2.0 gety would return 1.0 1.0 getWidth would retu

//If you need any help regarding this solution...... please leave a comment..... thanks

Add a comment
Know the answer?
Add Answer to:
please do in java and comments the code so i can understand Requirements: Create a Java...
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
  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

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
ADVERTISEMENT