Question
JAVA You will now create a Grid Class that contains an array of Rectangle objects. The Grid class has three attributes (an array of Rectangles,
JAVA
You will now create a Grid Class that contains an array of Rectangle objects.
The Grid class has three attributes (an array of Rectangles, the size of the array, and the current number of Rectangles in the array). When you create a Grid, you will pass in the max size of the array, initialize the array to the given maximum size and set the current number of Rectangles to 0).
Methods
- Get and set methods
- toString method that prints the current number of Rectangle objects and all the Rectangle objects in the Array
- addRectangle that takes in a Rectangle object and adds it to the array (if there is room)
- isHighest that returns the Rectangle object that has the highest point (or the first Rectangle stored in the array with the highest height if there are more than one Rectangle with the same highest point).
- largestArea returns the Rectangle with the largest area in the array (or the first Rectangle in the array with the largest area if there are more than one Rectangle with the same largest area)
- averageArea returns a double that represents the average area of all the Rectangles in the array.
You can add other methods if necessary and they make sense in all classes.
Details
Now, create a GridDemo class.
- Use a Scanner object to first read in the maximum number of Rectangles that the Grid class can store.
- Then read the attributes for four Rectangle objects (4 Points per Rectangle) and add these to the Grid.
- Print the Grid object (using the toString method)
- Print the highest Rectangle object
- Print the Rectangle object with the largest area
- Print the average area of all the Rectangles (to 2 decimal places using printf)
Details
Input
Size of the array in Grid, and then 4 points per 4 Rectangles Sample Input: 5 2 7 2 2 8 7 8 2 4 6 4 4 6 6 6 4 3 8 3 1 5 8 5 1 6 3 6 1 10 3 10 1
Output
Sample Output: Current Number of Rectangles: 4 Rectangle: (2, 7) (8, 7) (2, 2) (8, 2) Rectangle: (4, 6) (6, 6) (4, 4) (6, 4) Rectangle: (3, 8) (5, 8) (3, 1) (5, 1) Rectangle: (6, 3) (10, 3) (6, 1) (10, 1)
Highest Rectangle: Rectangle: (3, 8) (5, 8) (3, 1) (5, 1) Largest Area: Rectangle: (2, 7) (8, 7) (2, 2) (8, 2) Average area: 14.00
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started