/*
 * Stack.java
 *
 * Version:
 *     $Id$
 *
 * Revisions:
 *     $Log$
 */

/**
 * 
 * The Stack class represents a last-in-first-out (LIFO) stack of
 * objects. 
 * 
 * When a stack is first created, it contains no items. 
 *
 * @author      Nan Schaller
 */

public class Stack {

  /**
   * Class constant indicating an empty stack
   */
  private final static int EMPTY = 0;

  //HOW big to make it?
  private final int STACK_SIZE = 100;		// Default Stack Size
  private int top;				// Stack size
  private Object theStack[];			// The Stack

  /** 
   * Creates an empty Stack.
   */

  public Stack() {
    
    this( STACK_SIZE );

  }

  /** 
   * Creates an empty Stack of the specified size.
   *
   * @param	size	The size of the stack.
   */

  public Stack(int size) {

    top = EMPTY;
    theStack = new Object[size];

  }

  /**
   * Tests if this stack is full.
   *
   * @return	true if and only if this stack is full;
   *            false otherwise.
   */

  public boolean isFull() {

    return top == theStack.length;

  }

  /**
   * Tests if this stack is empty.
   *
   * @return	true if and only if this stack contains no items; 
   *		false otherwise.
   */
  public boolean isEmpty() {

    return top == EMPTY;

  }

  /**
   * Looks at the object at the top of this stack without removing 
   * it from the stack.
   *
   * @return	the object at the top of this stack; 
   */

  public Object peek() {

    Object returnValue = new Integer( EMPTY );

    if ( ! isEmpty() ) {
       returnValue = theStack[top-1];
    }

    return returnValue;

  }

  /**
   * Removes the object at the top of this stack and
   * returns that object as the value of this function.
   *
   * @return	the object at the top of this stack; 
   *            if the stack is empty, returns EMPTY 
   */

  public Object pop() {

    Object returnValue = new Integer( EMPTY );

    if ( ! isEmpty() ) {

      top--;
      returnValue = theStack[top];

    }

    return returnValue;
  }

  /**
   * Pushes an item onto the top of this stack.
   *
   * @param 	item 	the item to be pushed onto this stack.
   *
   */

  public void push(Object item){

    // WHAT happens if Stack is full?  Is it what the user has a right
    // to expect?
    // Should automatically grow theStack.

    if ( !isFull() ) {
       theStack[top] = item;
       top++;
    }

  }

} // Stack

