109 Program Better Rectangle The Rectangle class of the stan

10.9 Program: Better Rectangle

The Rectangle class of the standard Java library does not supply a method to compute the area or the perimeter of a rectangle.

Create a subclass BetterRectangle of the Rectangle class that has getPerimeter() and getArea() methods. Neither of these methods will receive values but will return a double value. Do not add any instance variables to your BetterRectangle class. In the parameterized constructor of the BetterRectangle class, call the setLocation and setSize methods of the Rectangle class in order to set the x,y coordinates of the upper left-hand corner of the Rectangle as well as its width and height.

In the main() of the RectangleChecker class, you will:

Ask the user to enter in the x and y coordinates as well as the height and width of a Rectangle.

Create a BetterRectangle object passing the user provided values to the parameterized constructor.

Test your getPerimeter() and getArea() methods on the BetterRectangle object and output the results.

Example output:

with user provided values of x=0, y=0, width=5, height=8 Example output:

Solution

import java.awt.Rectangle;

@SuppressWarnings(\"serial\")

public class BetterRectangle extends Rectangle {

    public BetterRectangle(int x, int y, int width, int height) {

        super(x, y, width, height);

    }

     

    /*public BetterRectangle(int x, int y, int width, int height) {

        super.setLocation(x, y);

        super.setSize(width, height);

    }*/

    public double getPerimeter() {

        return super.getHeight() * 2 + super.getWidth() * 2;

    }

     

    public double getArea() {

        return super.getHeight() * super.getWidth();

    }

}

10.9 Program: Better Rectangle The Rectangle class of the standard Java library does not supply a method to compute the area or the perimeter of a rectangle. Cr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site