import java.util.Vector; /** * IStack implementation using a Java Vector to hold its elements. * @author Jeff Raab */ public class VectorStackSolution implements IStack { /** Vector used to hold the elements on this. */ protected Vector elements = new Vector(); /** Constructs a new Java stack. */ public VectorStackSolution() {} /** * Pushes the given object onto this. * @param anObject object to push */ public void push(Object anObject) { elements.add(anObject); } /** Removes and returns the top object on this. */ public Object pop() { if (isEmpty()) { throw new RuntimeException("Stack is empty"); } return elements.remove(size() - 1); } /** Returns the top object on this. */ public Object top() { if (isEmpty()) { throw new RuntimeException("Stack is empty"); } return elements.elementAt(size() - 1); } /** Returns the number of objects on this. */ public int size() { return elements.size(); } /** Returns whether or not this is empty. */ public boolean isEmpty() { return elements.isEmpty(); } }