/**
  * O(1) time.
  */
  public int size() { 
    return (top + 1);
  }
 /**
  * O(1) time.
  */
  public boolean isEmpty() {
    return (top < 0);
  }
 /**
  * O(1) time.
  * @exception FullStackException if the array is full.
  */
  public void push(Object obj) throws FullStackException {
    if (size() == capacity)
      throw new FullStackException("Stack overflow.");
    S[++top] = obj;
  }
 /**
  * O(1) time.
  */
  public Object top() throws EmptyStackException {
    if (isEmpty())
      throw new EmptyStackException("Stack is empty.");
    return S[top];
    }
 /**
  * O(1) time.
  */
  public Object pop() throws EmptyStackException {
    Object elem;
    if (isEmpty())
      throw new EmptyStackException("Stack is Empty.");
    elem = S[top];
    S[top--] = null; // dereference S[top] for garbage collection.
    return elem;
  }
}