Results 1 to 9 of 9
  1. #1
    shogunduk is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Need help with this assignment

    Hi guys! First post and I need help with 2 problems in an assignment, or at least a hint on how to do them if homework help isn't allowed :p


    First problem

    Java Code:
    import java.awt.Rectangle ; 
    import java.util.Random ;
    
    /**
       Calling the rectangle constructor with numbers for x, y, width, height.
     */
    public class IntersectionPrinter
    {
        public static void main(String[] args)
        {
    	Random random = new Random() ;
    	Rectangle box1 = new Rectangle(100, 200, 300, 400) ;
    	//Reminder: do not write or change code outside of the todo regions.
    	//----------------------Start here. Do not remove this comment.
    	// todo: 1. make Rectangle box2 so that (a) the top left corner of box2 is in the center of box1, (b) the width of box2 is half the width of box1, and (c) the height of box2 is half the height of box1.
    	// approximate lines of code required: 1
    	
    	Rectangle box2 = new Rectangle(250, 0, 150, 200) ;
    	
    	//----------------------End here. Do not remove this comment.
    
    	Rectangle box3 = box1.intersection(box2) ;
    	System.out.println(box2.equals(box3)) ;
        }
    Ok, so for this one I drew out the rectangle (100, 200, 300, 400) and what I get as the rectangle it's asking for is: Rectangle box2 = new Rectangle(250, 0, 150, 200) ;

    But this returns as false so I'm not sure what I'm doing wrong.


    Second Problem

    Java Code:
    import java.awt.Rectangle ; 
    import java.util.Random ;
    
    /**
       Practice using getX, getY, getWidth and getHeight
     */
    public class IntersectionTester
    {
        public static void main(String[] args)
        {
    	Random random = new Random() ;
    	Rectangle box1 = new Rectangle(2 * random.nextInt(50), 
    				       2 * random.nextInt(50),
    				       2 * random.nextInt(100),
    				       2 * random.nextInt(100)) ;
    	//Reminder: do not write or change code outside of the todo regions.
    	//----------------------Start here. Do not remove this comment.
    	// todo: 1. Make a second rectangle, called box2, so that the top left corner of box2 is in the center of box1, the width of box2 is half the width of box1, and the height of box2 is half the height of box1.
    	// approximate lines of code required: 4
    	
    	
    
    	//----------------------End here. Do not remove this comment.
    
    	Rectangle box3 = box1.intersection(box2) ;
    	System.out.println(box2.equals(box3)) ;
        }
    }
    Yeah, I have absolutely no idea how to do this one. I tried making a box2 with box1.getX(), box1.getY(), etc, etc as the 4 parameters but that gave me a bunch of errors.

    Edit* For the second problem I put Rectangle box2 = new Rectangle(box1.getX(), box1.getY(), box1.getWidth(), box1.getHeight()); (which isn't even close since it asks for 4 lines of code) into the answer section and the error I get when I compile it is

    IntersectionTester.java:21: error: no suitable constructor found for Rectangle(d
    ouble,double,double,double)
    Rectangle box2 = new Rectangle(box1.getX(), box1.getY(), box1.getWidth()
    , box1.getHeight());
    ^
    constructor Rectangle.Rectangle(Dimension) is not applicable
    (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(Point) is not applicable
    (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(Point,Dimension) is not applicable
    (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(int,int) is not applicable
    (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle(int,int,int,int) is not applicable
    (actual argument double cannot be converted to int by method invocation co
    nversion)
    constructor Rectangle.Rectangle(Rectangle) is not applicable
    (actual and formal argument lists differ in length)
    constructor Rectangle.Rectangle() is not applicable
    (actual and formal argument lists differ in length)
    1 error


    Any help would be much appreciated. Thank you!
    Last edited by shogunduk; 09-23-2012 at 03:49 AM.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Need help with this assignment

    Please put your code in [code ][/code ] tags.
    For the second problem post the errors here so we can see what you have done wrong.
    Last edited by PhQ; 09-23-2012 at 03:39 AM.

  3. #3
    shogunduk is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help with this assignment

    Done, I hope I did it right :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with this assignment

    Regarding the first problem...

    Looking at the y component of (100,200,300,400) the rectangle goes from y=200 to y=600. So I don't see how your answer of (250,0,150,200) can possibly be right as it puts the top of your rectangle at y=0 which is right outside the rectangle you were given rather than in the middle.

    ---

    Try editing your code to put the tags in so it's readable.

    You put [code] at the start of a section of code and [/code] at the end.

    [Edit]slow... Well done!

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Need help with this assignment

    Quote Originally Posted by shogunduk View Post
    Done, I hope I did it right :)
    Thank you !!!
    On the second problem you get the error because the getX(), getY(), getHeight(), and getWidth() methods return a double and the Rectangle constructor requires ints. (http://docs.oracle.com/javase/6/docs...Rectangle.html)

    You can use the x, y, height and width variables instead.
    Last edited by PhQ; 09-23-2012 at 03:57 AM.

  6. #6
    shogunduk is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help with this assignment

    Thank you so much, both of you!

    I was visualizing the rectangle the wrong way for the first problem, going 400 negative instead of positive for the height.
    And I totally forgot about double and int.

    Thank you!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with this assignment

    You're welcome.

    I thought you were getting the height backwards. The "y downwards" convention of computer graphics affects how things are rendered on the screen, but doesn't change how we do arithmetic with coordinates. Any more than a mirror placed along the top of the monitor would.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Need help with this assignment

    shogunduk, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Need help with this assignment

    noman.21, when you have a question start your own thread. Don't hijack another poster's thread. Your post in this thread has been removed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help with assignment.
    By smileybear33 in forum New To Java
    Replies: 2
    Last Post: 04-26-2011, 03:47 AM
  2. Help with an assignment
    By aqeel2010 in forum New To Java
    Replies: 5
    Last Post: 01-14-2011, 09:04 AM
  3. Need help with a assignment
    By helpMe.Java in forum New To Java
    Replies: 7
    Last Post: 06-06-2010, 04:49 PM
  4. Assignment
    By shinkymar in forum New To Java
    Replies: 2
    Last Post: 03-06-2010, 01:17 PM
  5. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •