Last modification: 14/11/16
The Movable
interface.
Write a public geometry.Movable
interface. It declares a move()
method that takes two integer distances (vertical and horizontal) as arguments. It has no return value.
The Figure
interface.
Write a public geometry.Figure
interface. It extends the above Movable
interface and the java.lang.Comparable<Figure>
interface with the methods below.
An area()
that calculates the area of the figure.
A show()
that returns the string representation of the figure.
A default implementation of the method compareTo()
of the Comparable
interface. The default implementation compares two figures based on their areas. The greater figure has greater area.
The Circle
class.
Write a public geometry.Circle
class that represents a circle. The class implements the methods of the interface Figure
.
Declare three private int
fields, the radius and the coordinates of the center of the circle respectively. Make the radius a read-only field using final
.
Define a constructor which takes the initial values of the fields and initializes them.
Define the methods of the interface Figure
. These are move()
, area()
and show()
.
The show()
may produce the following result: Circle at (2,4) radius 5
.
The Square
class.
Write a public geometry.Square
class which represents a square. The class also implements the methods of the interface Figure
.
Declare three private int
fields, the size of sides and the coordinates of the upper left corner. Make the size of sides a read-only field using final
.
Define a constructor which takes the initial values of the fields and initializes them.
Implement the methods of the interface Figure
. These are move()
, area()
and show()
.
The show()
may produce the following result: Square at (2,0) side 4
.
The FigureDemo
class.
Write a FigureDemo
class outside of the geometry
package.
Define a static method largest()
which returns the figure with largest area from a sequence of figures. The largest()
accepts a ArrayList
and a LinkedList
as well, so it takes a List
as argument.
Use the compareTo()
method of the interface Figure
to select the largest figure, and an Iterator
to iterate over the list.
Write a main()
method to test your largest()
method. Initialize a List
variable with a LinkedList
object and add some figures to it. Print the largest figure to the output.