# Question on Rectangles

• March 21st, 2012, 10:34 PM
parkBENch
Question on Rectangles
Hi there,

I am to code a method that creates a rectangle (r3) which is the area of two intersected rectangles (r1, r2).
r3 must be in (x, y, width, height) format.
The first rectangle (r1) is randomly created (x, y, width, height - are all random inbetween 0-100)
The second rectangle (r2) is created by user input (x, y, width, height - entered by user between 0-100)

If there is an intersection, the program prints "r3(x, y, width, height)"
If there is no intersection the program prints "no intersection"

How would I go about doing this? A while loop inside an if statement?

Code :

```private void findIntersection(Rectangle r1) { r2 = new Rectangle(x, y, width, height); r2 = userRectangle(); //method for user input   if(r1.intersects(r2)) { //while loop? System.out.println("The intersection is "); }else{ System.out.println("No intersection"); } }```

Any hints or tips appreciated
Thanks :)
• March 21st, 2012, 11:13 PM
pbrockway2
Re: Question on Rectangles
What is this Rectangle class?

If you are using java.awt.Rectangle then there is a method to find intersections. If not - ie if you are writing the Rectangle class - you should concentrate on writing a method that produces a new Rectangle by intersection with some given rectangle.

Then once you have such a Rectangle method (or using the java.awt.Rectangle one) you can write the code to (a) get user input) (b) create rectangles (c)intersect them (d) output the result.

The point is to separate the user input code from the intersection code.

-----

I can't see anything in your statement of the problem that calls for a while loop: ie any action that repeats over and over again. Certainly finding the intersection of two rectangles does not involve any loops.