6.16. Exercises

You can download the source code for the examples in the book and the solutions to the following exercises from http://www.wrox.com.

  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, and 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 you 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 ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.