- If you haven’t completed Objects as Return Types, do so now.
- Open ObjectAsReturnType.java and add the following method.
public static void moveRect(Rectangle box, int dx, int dy){ box.x = box.x + dx; box.y = box.y + dy; }
- Add the following lines to the end of main.
moveRect(box,50,100); System.out.println(box.toString());
- Compile and run the program.
$ javac ObjectAsReturnType.java $ java ObjectAsReturnType java.awt.Rectangle[x=0,y=0,width=100,height=200] java.awt.Point[x=50,y=100] java.awt.Rectangle[x=50,y=100,width=100,height=200]
The complete class should appear as follows.
import java.awt.Rectangle; import java.awt.Point; public class ObjectAsReturnType{ public static void main(String[] args){ Rectangle box = new Rectangle(0,0,100,200); Point myPoint = ObjectAsReturnType.findCenter(box); System.out.println(box.toString()); System.out.println(myPoint.toString()); moveRect(box,50,100); System.out.println(box.toString()); } public static Point findCenter(Rectangle box){ int x = box.x + box.width / 2; int y = box.y + box.height / 2; Point retPoint = new Point(x,y); return retPoint; } public static void moveRect(Rectangle box, int dx, int dy){ box.x = box.x + dx; box.y = box.y = dy; } }