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:

[B]Rectangle.java:[/B]
 
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;
 }
}
 
 
[B]RectangleTest3.java[/B]
 
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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.