import java.util.EmptyStackException; public class MyStack implements StackInterface { MyStack() { data = (E[]) new Object[3]; makeEmpty(); } public void makeEmpty() { top = -1; } public boolean empty() { return (top == -1); } public E push (E item) { top++; if (top == data.length) { E [] newData = (E[]) new Object [data.length * 2]; for (int i = 0; i < data.length; i++) { newData[i] = data[i]; } data = newData; } data[top] = item; return item; } public E pop()throws EmptyStackException { if (empty()) { throw new EmptyStackException(); } else { top--; return(data[top+1]); // notice the top + 1 } } public E peek() throws EmptyStackException { if (empty()) { throw new EmptyStackException(); } else { return(data[top]); } } private E [] data; private int top; }