public class ArrayStack { private int capacity; // stack capacity public static final int CAPACITY = 100; // default stack capacity private E[] S; // E array used to implement the stack private int tos = -1; // index for the top of stack public ArrayStack(){this(CAPACITY);} @SuppressWarnings("unchecked") public ArrayStack(int cap){ capacity = cap; S = (E[]) new Object [capacity]; } public int size(){return tos+1;} public boolean isEmpty(){return tos == -1;} public void push(E element) throws StackException { if (size() == capacity) throw new StackException("Stack overflow"); tos++; S[tos] = element; } public E top() throws StackException{ if (isEmpty()) throw new StackException("Stack empty"); return S[tos]; } public E pop() throws StackException { if (isEmpty()) throw new StackException("Stack underflow"); E element = S[tos]; S[tos] = null; tos--; return element; } public String toString(){ String s = "["; if (tos >= 0) s = s + S[0]; for (int i=1;i<=tos;i++) s = s +","+ S[i]; return s + "]"; } }