Question: For this lab, a Rectangle is defined two points: upper left point and lower right point. These points are of the type Point with the

For this lab, a Rectangle is defined two points: upper left point and lower right point. These points are of the type Point with the value of x and y.With these two points, the height and width can be computed. For example, the height would be the upper left point's y - the lower right point's y. You can assume the rectangles are always horizontal as shown in the picture below.
Write a java program that prompts the user for N random rectangles, create an array of N rectangles and print them.
Do the following:
Write a Rectangle class in a file named Rectangle.java
Add private instance variables upperLeftof type int to the Rectangleclass
Add private instance variables lowerRightof type int to the Rectangleclass
Add a empty constructor that initializes the rectangle's upperLeft and lowerRightto the value (,0,0).
Add a parameterized constructor that initialize the rectangle's upperLeftand lowerRightwith the parameters' values.
Add a method getHeight() that returns the height of the the rectangle.
Add a methodgetWidth()that returns thewidthof the width rectangle.
Add a method setUpperLeft(Point ul) that updates the instance variable upperLeft
Add a methodsetLowerRight(Point ul)that updates the instance variablelowerRight
Add a method getUpperLeft()that returns the upperLeft point of the the rectangle.
Add a methodgetLowerRight()that returns the lowerRightpointof the the rectangle.
Add a method getArea() that returns the area of the rectangle.
Override the toString() method to returns the string representation of the rectangle object. For example, if the rectangle has a height of 5 and a width of 3, then returned string is "Rect[h=5, w=3]"
Create a method in Main class called randRect(int n) returns an array of n random rectangles. Random means that the upper left and lower right of each rectangle object is randomly generated.
In the main method, prompts for the number of rectangles. Then use the method randRect(int n) to generate the random rectangles. Then print each rectangles and its area as shown in the sample run.
Sample run:
How many rectangles? 10
Rect[h=10, w=1] has an area of 10
Rect[h=8, w=10] has an area of 80
Rect[h=2, w=4] has an area of 8
Rect[h=2, w=5] has an area of 10
Rect[h=8, w=2] has an area of 16
Rect[h=6, w=10] has an area of 60
Rect[h=9, w=4] has an area of 36
Rect[h=2, w=10] has an area of 20
Rect[h=6, w=10] has an area of 60
Rect[h=3, w=2] has an area of 6

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 Programming Questions!