Question: In lecture we built a bounded array-based stack. But you can imagine a stack whose backing array grows as needed. Suppose we were going to
In lecture we built a bounded array-based stack. But you can imagine a stack whose backing array grows as needed. Suppose we were going to modify the BoundedArrayStack to be an UnboundedArrayStack that doubles the size of the backing array instead of throwing a StackOverflowException.
1. Update the method(s) of BoundedArrayStack required to make it into an UnboundedArrayStack; show the full signature and body of any method(s) you modify or add. Be sure your new method(s) still implement the interface!
public class BoundedArrayStackimplements Stack { private E[] array; private int top; public BoundedArrayStack(int capacity) { top = -1; array = (E[]) new Object[capacity]; // references to this array must not escape this class } @Override public void push(E e) throws StackOverflowException { if (isFull()) { throw new StackOverflowException(); } top += 1; array[top] = e; } @Override public E pop() throws StackUnderflowException { if (isEmpty()) { throw new StackUnderflowException(); } E temp = array[top]; array[top] = null; top--; return temp; } @Override public E peek() throws StackUnderflowException { if (isEmpty()) { throw new StackUnderflowException(); } return array[top]; } @Override public boolean isEmpty() { return top == -1; } @Override public int size() { return top + 1; } @Override public boolean isFull() { return top == array.length - 1; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
