/**
 * @invariant size()<=MAXSIZE  (An invaraint of a class is a formally specified 
 *  condition that always holds on any object of the class whenever it is 
 *  in a stable state.)
 */
public class StackImpl implements Stack {
  protected static int MAXSIZE=50;
  protected Object[] element;
  protected int topIndex;

  public StackImpl() {
    element=new Object[MAXSIZE];
    topIndex=0;
  }

  /**
   * adds a new object onto the top of the stack
   *
   * @pre true
   * @post size()==size()@pre+1
   * @post top()==obj@pre
   */
  public void push(Object obj) {
    assert size()<=MAXSIZE;
    element[topIndex]=obj;
    topIndex++;
  }

  /**
   * removes the object at the top of the stack and return the object
   *
   * @pre size()>0
   * @post size()==size()@pre-1
   * @post @result==top()@pre
   */
  public Object pop() {
    assert size()>0;
    topIndex--;
    Object result=element[topIndex];
    return result;
  }

  /**
   * returnes the numbers of objects currently in the stack
   *
   * @pre true;
   * @post @nochange
   */
   public int size() {
    return topIndex;
  }

  /**
   * returns the object at the top of the stack without removing it from the stack
   *
   * @pre size()>0
   * @post @nochange
   */
  public Object top() {
    assert size()>0;
    return element[topIndex-1];
  }
}
