public class ArrayStack // Array implementation of a stack, since we are using an array, we cannot store a Generic, so we will just make it an array of ints { private int[] stack; // our stack private int top; // index of the current top of the stack public ArrayStack() // no size, set it to 100, top is initially -1 { stack=new int[100]; top=-1; } public ArrayStack(int size) // user gave us a size, this limits the array to a reasonable amount but we will have full stacks if top==size { stack=new int[size]; top=-1; } public int peek() throws StackEmptyException // return top of stack (if empty, throw exception) { if(top==-1) throw new StackEmptyException("Stack is empty, cannot peek"); else return stack[top]; } public int pop() throws StackEmptyException // return top of stack and reduce top by 1 for new top of stack { if(top==-1) throw new StackEmptyException("Stack is empty, cannot pop"); else { int temp=stack[top]; top--; return temp; } } public boolean isEmpty() // stack is empty if top is -1 as top will be the index of the top of stack, between 0 and size-1 { return top==-1; } public boolean isFull() // unlike the linked list implementation, the array implementation can be full (unless we implement array doubling) { // full occurs once top==size-1 return top==stack.length-1; } public void push(int x) throws StackFullException // add x onto stack at position top+1 as long as stack is not yet full { if(top==stack.length-1) throw new StackFullException("Cannot push " + x + " onto full stack"); else { top++; stack[top]=x; } } }