public class ArrayStack implements StackInterface

{

        private Object[] stack; // array of stack entries

        private int topIndex;   // index of top entry

        private static final int DEFAULT_MAX_SIZE = 50;

 

        public ArrayStack()

        {

                stack = new Object[DEFAULT_MAX_SIZE];

                topIndex = -1;

        } // end default constructor

 

        public ArrayStack(int maxSize)

        {

                stack = new Object[maxSize];

                topIndex = -1;

        } // end constructor

 

        public void push(Object newEntry)

        {

 

          if (isFull())

                throw new IllegalStateException("Attempt to push element into a full stack.");

 

              stack[++topIndex] = newEntry;

          } // end push

      public Object peek()

      {

              Object top = null;

 

              if (!isEmpty())

                      top = stack[topIndex];

 

              return top;

      } // end peek

      public Object pop()

      {

              Object top = null;

 

              if (!isEmpty())

              {

                      top = stack[topIndex];

                      stack[topIndex] = null;

                      topIndex--;

              } // end if

 

              return top;

      } // end pop

      public boolean isEmpty()

      {

              return topIndex < 0;

      } // end isEmpty

 

      private boolean isFull(){

        return topIndex == stack.length - 1;

      }

 

  /**

   * clear

   */

  public void clear() {

    topIndex = -1;

  }

}