Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Rectangle questions

Hybrid View

  1. #1

    Default 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:
    import java.awt.Rectangle;
    public class InsertionPrinter
    public static void main(String[] args)

    //construct box1
    Rectangle box1 = new Rectangle(0, 0, 20, 40);
    //label box1 to r1
    Rectangle r1 = box1;

    //construct box2
    Rectangle box2 = new Rectangle(10, 20, 40, 60);
    //label box2 to r2
    Rectangle r2 = box2;

    //calculate intersected region
    Rectangle r3 = r1.intersection(r2);
    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.

  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Tavistock, UK

    Default 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?

  3. #3

    Default 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.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Tavistock, UK

    Default 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.

Similar Threads

  1. Dragging and Dropping Rectangle - GUI
    By cjct927 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 8th, 2011, 09:59
  2. grow a rectangle in java
    By Khoatic in forum Java Theory & Questions
    Replies: 2
    Last Post: September 8th, 2010, 18:13
  3. How to form a rectangle from the union of two Rectangles.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 3rd, 2010, 07:22
  4. Fill in rectangle partially (clip?)
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 5
    Last Post: March 27th, 2010, 13:38
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 07:22