# Rectangle questions

Printable View

• September 5th, 2011, 01:01 AM
stacksofamber
Rectangle questions
I'm completely ignorant on JAVA and am currently attempting to learn it. I'm finding it rather confusing. I've googled my question that is on my assignment and it seems that it is a common question. Here it is:

The intersection method computes the intersection of two rectangles, that is the rectangle that is formed by two overlapping rectangles.
You call this method as follows

Rectangle r3 = r1.intersection(r2)

Write a program IntersectionPrinter that constructs two rectangle objects, prints them, and then prints the rectangle object that describes the intersection. Then the program should print the result of the intersection method when the two rectangles do not overlap. Add a comment to your program that explains how you can tell whether the resulting rectangle is empty.

Here's part of my answer:
Code Java:

```import java.awt.Rectangle; public class InsertionPrinter { public static void main(String[] args) {   //construct box1 Rectangle box1 = new Rectangle(0, 0, 20, 40); System.out.println(box1); //label box1 to r1 Rectangle r1 = box1;   //construct box2 Rectangle box2 = new Rectangle(10, 20, 40, 60); System.out.println(box2); //label box2 to r2 Rectangle r2 = box2;   //calculate intersected region Rectangle r3 = r1.intersection(r2); System.out.println(r3); }}```
So, I was able to figure out how to construct the two rectangles, print them, and print the rectangle object that describes the intersection. I'm confused by the next part of the question. If my two rectangles intersect (and in my case, overlap), then how can I print the result of the intersection method when the two rectangles do not overlap? Rectangles cannot simultaneously overlap and not overlap. Have I done something incorrectly? Are they supposed to only intersect and not overlap? If so, how do I go about doing that? Also, please keep in mind that I am only in chapter 2 of my text book and according to my teacher, we are learning the difficult stuff before he will teach us the easy stuff, so if there are shortcuts or easier ways, I probably haven't learned them yet and can't use them on my homework. I hope I have included all the information necessary for someone to help me. Thanks so much for any help given.
• September 5th, 2011, 05:44 AM
Sean4u
Re: Rectangle questions
Have you tried changing the dimensions of the two hard-coded rectangles in your code so that they don't intersect? What happened?
• September 5th, 2011, 11:28 AM
stacksofamber
Re: Rectangle questions
So, I can certainly do that but that still leaves me confused. This intersection method I'm supposed to use "computes the intersection of of two rectangles--that is, the rectangle that is formed by two overlapping rectangles". If I changed the dimensions, then the second part of the question would make sense...I would be able to design a program for rectangles that do not intersect. But then the first part of the question cannot be answered because then there would not be a way for me to write a program that ends with a rectangle object that describes the rectangle formed when these same two rectangles overlap. My whole problem is that in reading the problem, it seems like I'm being asked to use the same rectangles to write separate programs--one that more or less describes the rectangle that is formed by overlapping and one that more or less shows that the rectangles do not overlap. How can the same two rectangles overlap and not overlap at the same time? I feel like I'm missing something very basic because this question cannot be as hard as it seems and I'm sure I will feel dumb when I do find out the correct answer. If I'm approaching this whole thing the wrong way, feel free to let me know. Any help is appreciated. Also, upon asking the teacher for help, all I get as a response is something like, "We're learning the hard stuff now but it'll get easier"...as he walks away from me, still not answering my question. So, I have tried to get help in class and I'm sort of out of options at this point. Thanks for any help.
• September 5th, 2011, 12:43 PM
Sean4u
Re: Rectangle questions
Ah ok - you're right, the question is crap. At least, it's badly worded: "*the* two rectangles" would normally refer to the two rectangles previously created, but I think the intention is for you to create two rectangles that *do* overlap and print the intersection, *then* to create two new rectangles (or possibly only one new one) which do not overlap and to again print the result of the intersection method.

It's well worth reading the API doc for Rectangle.intersection(Rectangle) - it explains what the return value will be when the two Rectangles don't overlap. I think this is as much as anything else to test your ability to read the API docs. The intersection method documentation does have the answer to the first part of the second question, and there is a method in Rectangle which is the perfect answer to the second part.

I think that's the best way to interpret the question. It's certainly misleadingly written if you read it logically. If your teacher says you've got the right answer in the end, perhaps you should suggest to them that they wrote the wrong question and draw their attention to their careless use of the definite article.