Question: JAVA This is combination of p9.10 and 9.11. The Rectangle class of the standard Java library does not supply a method to compute the area

JAVA

This is combination of p9.10 and 9.11.

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

(1) default constructor, which sets the coordinate of the upper corner of the rectangle to be (50, 50), i.e. x = 50, y = 50, and the width and height of the rectangle to be 50, 50.

(2) second constructor with 4 parameters, which are the initial values for x, y, width and height

(3) getArea method, which returns the area of the rectangle

(4) override the toString method, so it prints out information including area

(5) complete the main method

public static void main(String[] args)

{

// declare a variable of BetterRectangle b

// create a BetterRectangle using default constructor, assign it to b

System.out.println(b); // implicitly call b.toString()

// reset the location of the object to x = 10, y = 20

// reset the location of the object so width = 100, height = 120

System.out.println(b); // implicitly call b.toString()

// create a BetterRectangle using the second constructor so that x = 10, y = 10, width 40, height= 6, assign to b

System.out.println(b);

}

(6) test and run the program, the expected output should be as follows:

BetterRectangle[x=50,y=50,width=50,height=50] area: 2500.0

BetterRectangle[x=10,y=20,width=100,height=120] area: 12000.0

BetterRectangle[x=10,y=10,width=40,height=60] area: 2400.0

(7) to use the Rectangle class which is in the package java.awt, you need to import the package at the beginning of the file

import java.awt.*;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!