class Stack2 { private int maxSize; // size of stack array private long[] stackArray; private int top; // top of stack //-------------------------------------------------------------- public Stack2(int s) // constructor { maxSize = s; // set array size stackArray = new long[maxSize]; // create array top = -1; // no items yet } //-------------------------------------------------------------- public void push(long j) throws StackOverflowException // put item on top of stack { if( isFull() ) { throw new StackOverflowException( ); } stackArray[++top] = j; // increment top, insert item } //-------------------------------------------------------------- public long pop() throws StackUnderflowException // take item from top of stack { if ( isEmpty() ) { throw new StackUnderflowException( ); } return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- public long peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- public boolean isFull() // true if stack is full { return (top == maxSize-1); } } class StackOverflowException extends Exception { } class StackUnderflowException extends Exception { } class Stack2App { public static void main( String [] args ) { Stack2 stack = new Stack2(10); try { stack.push( 54 ); } catch( StackOverflowException e ) { System.out.println( "Stack is full" ); } } }