User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 422,817 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,361 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1959 | Replies: 1
Reply
Join Date: Oct 2006
Posts: 1
Reputation: *Alice* is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
*Alice*'s Avatar
*Alice* *Alice* is offline Offline
Newbie Poster

Help Help regarding rectangle moving

  #1  
Oct 21st, 2006
Hey guys,

For one of my assignments, i got this little exercise:
"Add at the class Rectangle a method move(Point p) which
moves the rectangle in the point indicated by the parameter, a method
translate(int x, int y) which translates the rectangle to the right
along the 0x coordinate axis and downward along the 0y coordinate
axis, and a method setSize(int width, int height) which sets
the size of this rectangle to the specified width and height. Compile the
class Rectangle.
Define a class RectangleTest3 with the method main() which
creates a rectangle, moves, sets size and translates it. After each
operation the algorithm prints the coordinates of its left-top vertex, its
edges and area. Compile and execute RectangleTest3."
The problem I'm having is that i don't know if i interpreted the assignment correctly.
So far i have written this:

Rectangle.java:
 
import java.lang.Math;
public class Rectangle
{
 int x;
 int y;
 int height;
 int width;
 int ax;
 int ay;
 
 public Rectangle(int a, int b, int c, int d) 
 {
   height=c;
   width=d;
   ax=a;
   ay=b;
 }
 
 public Point move(int ax, int ay)
 {
   x = ax;
   y = ay;
   Point v = new Point(x, y);
   return v;
 }
}
 
 
RectangleTest3.java
 
public class RectangleTest3
{
 public static void main (String[] args)
 {
   Rectangle rec = new Rectangle(1, 3, 6, 7);
   System.out.println("Coordinates of left-top vertex: ("+rec.x+", "+rec.y+")");
   Point vertex = rec.move(rec.ax, rec.ay);
   System.out.println("Rectangle Area: ("+vertex.x+", "+vertex.y+")");
 }
}
 


I have taken some of the code out to simplify things (will add back in later) however I'm unsure if I am interpeting this problem right becasue after moveing the rectangle away from the origin, the rest of these steps don't make sense. I'd appreciate any helpful input.

Thanks,
Alice
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: bangalore
Posts: 1
Reputation: thekillerkat is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
thekillerkat's Avatar
thekillerkat thekillerkat is offline Offline
Newbie Poster

Re: Help regarding rectangle moving

  #2  
Jan 22nd, 2007
0..
i think you took few extra variables..
to represent a rectangle u need atmost 4 variables. which you can do in two easy ways..
0.0..
 
int TopLeftx,TopLefty;// to represent top left coordinates of the rectangle.
int Width,Height;//to represent the width and height of the rectangle respectively
0.1..
 
int TopLeftx,TopLefty;//same as previous
int BottomRightx,BottomRighty;// to represent bottom right x and y coordinates.
well these are not the only methods.. A more structured way would be to use two Point objects to represent the top left and bottom right coordinates of the rectangle.You can easily calculate the width and height of the rectangle from those.
->
Point TopLeft,BottomRight;

1..
ur assing.. says ->

"Add at the class Rectangle a method move(Point p) which
moves the rectangle
you did ->
public Point move(int ax, int ay)
{
   x = ax;
   y = ay;
   Point v = new Point(x, y);
   return v;
}
function header should be like this ->
public void move ( Point p)

2..
then ur asng.. says ->
method translate(int x, int y) which translates the rectangle to the right along the 0x coordinate axis and downward along the 0y coordinate axis

u wrote ->
nothing... :!:
translate(...) does nothing but it simply decreases the value of top left x coordinate value by the value passed to the function and increases the value of top left y coordinate coordinate by the corresponding value passed to the function (in case of screen coordinates , since here the (0,0) coordinate is the top right cornor or your screen).. keeping the width and height of the rectangle same..

{ they have used 0x and 0y as variable names in the problem discription.. you better use those names.. although thats not mandatory in general}

Try it urself..Its easy.

well.
Thats all for now.
-------thekillerkat--------
----death follows life----
---truth is undeniable---
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC