/** * This class implements a peekable generic Stack as a dynamic array; * a peekable stack allows the user to access any stack element. */ public class PeekableStack extends Stack { /* Note that there's no in the constructor definition */ PeekableStack(int initialSize) { super(initialSize); } /** * Peeks at the kth element, where the bottom of the stack has * position 1, and increases as we go up to the stack top. */ public T peek(int k) { if (k < 1 || size < k) throw new RuntimeException("Trying to peek at a nonexistent element."); return istack[size-k]; } }