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 yWith 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
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 methodgetWidththat returns thewidthof the width rectangle.
Add a method setUpperLeftPoint ul that updates the instance variable upperLeft
Add a methodsetLowerRightPoint ulthat updates the instance variablelowerRight
Add a method getUpperLeftthat returns the upperLeft point of the the rectangle.
Add a methodgetLowerRightthat 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 and a width of then returned string is "Recth w
Create a method in Main class called randRectint 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 randRectint n to generate the random rectangles. Then print each rectangles and its area as shown in the sample run.
Sample run:
How many rectangles?
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Recth w has an area of
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
