and create a new project named
Create in this project a package shapes and
add the class Rectangle
to it. Read the documentation about the abstract class java.awt.geom.Point2D
and comment the code of Rectangle.
Add a new class Test in the
default package with a main method and use it to create, move and print
- Following the example of Rectangle,
add a new class Square in
the package shapes. To
avoid the code duplication, propose two solutions, one based on an
abstract class, the second on delegation.
- Propose a solution to create a List in Test in
which you may put instances of Rectangle
but no other type of object.
- Add a new private method movingMessage()
which print "moving
to x, y" when the position of the rectangle is moved to a new
value (x, y). Modify the rest of the code of Rectangle
in such a way that movingMessage
is called each time the rectangle moves.
Now, we want to modify the method position()
in such a way that it returns a view
on the position of the rectangle. Note that the method movingMessage
must be called when you change the coordinates of the point returned by
For example, after the execution of the following instructions :
Rectangle r = new Rectangle(new Point2D.Double(10,10), 5, 5);
Point2D p = r.position();
the following message will appear on the standard output :
moving to 20, 20