Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

youll be working with java.awt.Rectangle and java.util.Scanner objects. Create a class called RectangleFun with a main method. The main method should start by constructing a

youll be working with java.awt.Rectangle and java.util.Scanner objects. Create a class called RectangleFun with a main method. The main method should start by constructing a Scanner object for System.in. Construct the Scanner before the loop do not construct multiple Scanner objects. The user should be prompted to enter a number indicating how many rectangles they want to enter. Keep prompting the user until they enter a positive integer number do not assume the user entered an integer. Say that number is n. You should create an array of n Rectangles. Then, ask the user to input coordinates for each rectangle. The user should be told which rectangle they are entering (numbered 1 through n, where n is the number entered earlier) and prompted to enter four integers on one line separated by spaces representing the upper left x, y and lower right x, y coordinates. Note that you will have to convert the coordinates into parameters appropriate to construct a Rectangle.

For each line entered, verify that the upper left coordinate is actually above and to the left of the lower right coordinate. In Javas coordinate space, this implies that the upper lefts x must be less than the lower rights x and the upper lefts y must be less than the lower rights y coordinate. The user should be prompted to enter the rectangle again if the coordinates are not correct by the rules above. For this part, you may assume that the user entered four integer coordinates in a row without any invalid characters.

Once all n rectangles are entered, you should print the following pieces of information:

The bounds of the smallest rectangle that would contain all the rectangles entered in terms of its upper left and lower right coordinates. HINT: Look at the union method of the Rectangle class. Print out the bounds in terms of its coordinates as in the example.

How many pairs of rectangles intersect. HINT: use a nested loop. Dont double count. For example, if rectangle A intersects rectangle B, dont count the fact that B also intersects A! And dont count the fact that a rectangle intersects itself.

The total area that all the rectangles cover but not counting overlapped areas more than once. For example, rectangles (0,0)-(2,2), (1, 1)-(3, 3) and (2,2)(4,4) would have a total area of 10 even though a naive count would be 12.

You must follow the format of this example session (bold text was entered by the user):

How many rectangles would you like to enter?

3

Please enter rectangle 1:

2 2 0 0

Sorry, that rectangle was entered incorrectly.

Please enter rectangle 1:

0 0 2 2

Please enter rectangle 2:

1 1 3 3

Please enter rectangle 3:

2 2 4 4

The bounds of the smallest rectangle containing the above rectangles: (0,0)-(4,4)

How many pairs of rectangles intersect: 2 Total area covered by all rectangles: 10

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions