Goal: Learn how to delegate responsibility to an existing container class.
Tutorial
Java's Vector is a container class which allows applications to store many objects of various types, and to retrieve the objects later. The Vector is both an array (in the sense of indexing) and a list (in the sense of insert/delete). Whenever you see a multiplicity of "many" (i.e. an asterick: *) in a UML diagram, it is usually implemented with a Vector because it can store many Objects.
Consider this API for Java's Vector class: API: Vector
and associated tutorial: ex5.pdf
Recall the Person class from Ex1: Person.java
Here is the same test program found in the tutorial: VectorTest.java
along with a demo
Note that the compilation of VectorTest.java (and your own Stack.java below) results in this error warning:
Note: VectorTest.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
This is alright for now, we'll see in the next exercise how to solve this.
Assignment
Copy Stack.java from Ex4 (with exceptions): cp ../ex4/Stack.java .
Modify Stack.java to use a Vector instead of an array. Do NOT continue to use the "top" of the stack. The push needs to add the Object to the end of the Vector, and pop still needs to access this particular spot. Consider how the size() method can be used.
Turn-In: Stack.java, including commented-out listing of resultant output. Note that this output had better be the same as in Ex1 because the testing main is still doing exactly the same operations as before.