•
•
•
•
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
![]() |
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:
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
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
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..
0.1..
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.
->
1..
ur assing.. says ->
you did ->
function header should be like this ->
2..
then ur asng.. says ->
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.
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
int TopLeftx,TopLefty;//same as previous int BottomRightx,BottomRighty;// to represent bottom right x and y coordinates.
->
Point TopLeft,BottomRight;
1..
ur assing.. says ->
•
•
•
•
"Add at the class Rectangle a method move(Point p) which
moves the rectangle
public Point move(int ax, int ay)
{
x = ax;
y = ay;
Point v = new Point(x, y);
return v;
}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---
----death follows life----
---truth is undeniable---
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Moving objects on a form (VB.NET)
- Annoying problem with moving files (Windows NT / 2000 / XP / 2003)
- Mouse Moving? (Windows 9x / Me)
- Moving to a larger case (Troubleshooting Dead Machines)
Other Threads in the Java Forum
- Previous Thread: how to convert digit(number) into word
- Next Thread: makingit52000


Linear Mode