O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.10. 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. A stack is a container that stores objects in a manner indicated by its name—in a vertical stack where only the object at the top of the stack is accessible. It works rather like a sprung stack of plates in a cafeteria. Only the top plate is at counter level and, therefore, is the only one you can access. When you add a plate to the stack, the existing plates are pushed down so the new plate is now the one that you can access. Define a generic Stack<> type with a method push() that adds the object that is passed as an argument to the top of the stack, and with a method pop() that removes and returns the object that is currently at the top of the stack. The pop() method should return null when the stack is empty. Demonstrate the operation of your Stack<> implementation by storing and retrieving 10 strings and 10 Double objects in stacks of a suitable type.

  2. Implement and demonstrate a listAll() method in the Stack<> class definition that will list the objects in the stack.

  3. Modify your Stack<> type to make it serializable. Demonstrate that this is the case by creating a Stack<String> object and adding 10 strings to it, then serializing and deserializing the Stack<String> object, and listing the contents of the deserialized stack.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required