import java.util.EmptyStackException; public class ArrayStack implements Stack { //Data protected Object[] items; protected int top; //Constructors public ArrayStack() { this(10); } public ArrayStack(int n) { items = new Object[n]; top = -1; } public ArrayStack(Object[] data) { this(data.length + 10); for(int i = 0; i < data.length; i++) push(data[i]); } //Stack methods implementation public boolean isEmpty() { return top == -1; } public void push(Object x) throws FullStackException { if(isFull()) throw new FullStackException("Stack is full!"); items[++top] = x; } public Object pop() throws EmptyStackException { Object result = top(); top--; return result; } public Object top() throws EmptyStackException { if(isEmpty()) throw new EmptyStackException(); return items[top]; } //Other methods public boolean isFull() { return top == items.length - 1; } public int capacity() { return items.length; } }