/*  StackVector.java             Authors: Koffman & Wolz
 *  A stack class implemented as a vector.
 */
import java.util.*;

public class StackVector extends Vector {
  /* push - pushes an item onto a stack
     precondition : item is defined
     postcondition: item is at the top of the stack
  */
  public void push(Object item) {
    addElement(item);
  }


 /* pop - pops an item from a stack
  * precondition : stack is defined and is not empty
  * postcondition: returns item at top of stack and
  *                removes it from the stack. Old second
  *                stack element is at top of stack.
  */
  public Object pop() {
     Object item;

     item = peek();                // Retrieve item at top
     removeElementAt(size() - 1);  // Remove top element
     return item;
  }


 /* peek - retrieves an item from a stack
  * precondition : stack is defined and is not empty
  * postcondition: returns item at top of stack without
  *                removing it.
  */
  public Object peek() {
    if (empty())
	    throw new NullPointerException();
    return elementAt(size() - 1);
  }


  /* isEmpty - tests whether a stack is empty
   * precondition : none
   * postcondition: returns true if stack is empty;
   *                otherwise, returns false.
   */

   public boolean empty() {
      return (size() == 0);
   }

   public static void main(String[] args) {
      StackVector s = new StackVector();
      s.push("first on stack");
      s.push("hello");
      s.push("bye");
      s.push("welcome");
      System.out.println(s.peek());
      s.pop();
      s.push("last on stack");

      System.out.println(s.toString());
   }

} // class StackVector
