Java Reference
In-Depth Information
If several classes implement a common interface, the methods declared as members of the
interface can be executed polymorphically.
❑
Exercises
1.
Define an abstract base class
Shape
that includes
protected
data members for the (x, y)
position of a shape, a
public
method to move a shape, and a
public abstract
method
show()
to output a shape. Derive subclasses for lines, circles and rectangles. Also define the
class
PolyLine
that you saw in this chapter with
Shape
as its base class. You can represent:
a line as two points, a circle as a center and a radius, and a rectangle as two points on
diagonally opposite corners. Implement the
toString()
method for each class. Test the
classes by selecting ten random objects of the derived classes, then invoking the
show()
method for each. Use the
toString()
methods in the derived classes.
2.
Define a class,
ShapeList
, that can store an arbitrary collection of any objects of subclasses
of the
Shape
class.
3.
Implement the classes for shapes using an interface for the common methods, rather than
inheritance from the superclass, while still keeping
Shape
as a base class.
4.
Extend the
LinkedList
class that we defined in this chapter so that it supports traversing the
list backwards as well as forwards.
5.
Add methods to the class LinkedList to insert and delete elements at the current position.
6.
Implement a method in the
LinkedList
class to insert an object following an object passed as an
argument. (Assume the objects stored in the list implement an
equals()
method that compares
the
This
object with an object passed as an argument, and returns
true
if they are equal.)