logo Practice-It logo


Language/Type: Java method basics println Random
Author: Marty Stepp (on 2010/12/28)

Write a 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:

w: 5, h: 6, area: 30
w: 10, h: 5, area: 50
w: 2, h: 8, area: 16
w: 4, h: 4, area: 16
w: 2, h: 9, area: 18
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.
w: 5, h: 2, area: 10
w: 6, h: 5, area: 30
w: 7, h: 6, area: 42
w: 8, h: 10, area: 80
Four rectangles of increasing area.
Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.