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
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
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!
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.
Re: Need help with this assignment
Done, I hope I did it right :)
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!
Re: Need help with this assignment
Quote:
Originally Posted by
shogunduk
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.
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!
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.
Re: Need help with this assignment
shogunduk, please go through the Forum Rules -- particularly the third paragraph.
db
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