Question: MUST BE DONE IN NETBEANS JAVA 5. (20 points) ArrayStack class represents array implementation of the Stack. Provided class has four methods that are not

 MUST BE DONE IN NETBEANS JAVA 5. (20 points) ArrayStack classrepresents array implementation of the Stack. Provided class has four methods that

MUST BE DONE IN NETBEANS JAVA

5. (20 points) ArrayStack class represents array implementation of the Stack. Provided class has four methods that are not implemented: push(T newEntry), peek(), clear() and pop(). Complete the implementation of these methods. /** Task: Retrieves the stack's top entry. * @return either the object at the top of the stack or null if * the stack is empty */ public T peek() { public class ArrayStack implements Stackinterface { private T[] stack; // array of stack entries private int toplndex; // index of top entry private boolean initialized = false; private static final int DEFAULT_INITIAL_CAPACITY = 50; private static final int MAX_CAPACITY = 10000; } /** Task: Removes all entries from the stack */ public void clear() { public ArrayStack() { this(DEFAULT_INITIAL_CAPACITY); } } public ArrayStack(int initialCapacity) { checkCapacity(initialCapacity); @ SuppressWarnings("unchecked") T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; toplndex = -1; initialized = true; } // end constructor //adds a new entry to the top of the stack public void push(T newEntry) { public void ensureCapacity(){ if (toplndex == stack.length-1) {// if array is full, expand array int newLength = 2 * stack.length; checkCapacity(newLength); Arrays.copyOf(stack, newLength); } } //check if there is enough capacity, if not throw an exception public void checkCapacity(int capacityLength) { if (MAX_CAPACITY

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!