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.
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.
Implement and demonstrate a listAll() method in the Stack<> class definition that will list the objects in the stack.
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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access