Solved

Programming
Write a Static Method Named RandomRects That Calculates and Displays

Question 5

Essay

Programming
Write a static method named randomRects that calculates and displays the area of randomly-generated rectangles. The width and height of each rectangle should be a randomly generated integer between 1 and 10 inclusive. Your method should keep generating random rectangles until an increasing sequence of four areas is printed. In other words, if the last four rectangles generated have areas of a1, a2, a3 and a4 such that a1 < a2 < a3 < a4, the method should print the final message and stop. So your method will generate at least 4 total rectangles but possibly many more, stopping only when it sees 4 in a row with areas in increasing order.
The following calls demonstrate your method's behavior. Your method should match this output format exactly:
 Call  randomRects ();  randomRects();  Output  w: 5, h: 6, area: 30 w: 5, h: 2, area: 10 w: 10, h: 5, area: 50  w: 6, h: 5, area: 30 w: 2, h: 8, area: 16  w: 7, h: 6, area: 42  w: 4, h: 4, area: 16  w: 8, h: 10, area: 80 w: 2, h: 9, area: 18  Four rectangles of increasing area.  w: 8, h: 3, area: 24 w: 7, h: 2, area: 14 w: 3, h: 10, area: 30 w: 7, h: 9, area: 63 w: 9, h: 8, area: 72 Four rectangles of increasing area. \begin{array}{l|l|l}\text { Call } & \text { randomRects (); } & \text { randomRects(); } \\\hline \text { Output } & \text { w: 5, h: 6, area: } 30 & \text { w: 5, h: 2, area: } 10 \\& \text { w: 10, h: 5, area: 50 } & \text { w: 6, h: 5, area: } 30 \\& \text { w: 2, h: 8, area: 16 } & \text { w: 7, h: 6, area: 42 } \\& \text { w: 4, h: 4, area: 16 } & \text { w: 8, h: 10, area: } 80 \\& \text { w: 2, h: 9, area: 18 } & \text { Four rectangles of increasing area. } \\& \text { w: 8, h: 3, area: } 24 & \\& \text { w: 7, h: 2, area: } 14 & \\& \text { w: 3, h: 10, area: } 30 & \\& \text { w: 7, h: 9, area: } 63 & \\& \text { w: 9, h: 8, area: } 72 &\\& \text { Four rectangles of increasing area. }& \end{array}

Correct Answer:

verifed

Verified

(three solutions shown)
public static vo...

View Answer

Unlock this answer now
Get Access to more Verified Answers free of charge

Related Questions