Question: /** An interface for the ADT stack. @author Frank M. Carrano @version 4.0 */ public interface StackInterface { /** Adds a new entry to the

/** An interface for the ADT stack. @author Frank M. Carrano @version 4.0 */ public interface StackInterface { /** Adds a new entry to the top of this stack. @param newEntry An object to be added to the stack. */ public void push(T newEntry); /** Removes and returns this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop(); /** Retrieves this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty. */ public T peek(); /** Detects whether this stack is empty. @return True if the stack is empty. */ public boolean isEmpty(); /** Removes all entries from this stack. */ public void clear(); } // end StackInterface

/** An interface for the ADT stack. @author Frank M. Carrano @version

Create class called ArrayBasedStack that implements the StackInterface.

Declare the following variables:

data: references an array storing elements in the list

topOfStack: an int value representing the location of the stack top in the array

INITIAL_CAPACITY: the default capacity of the stack set to 5

private T] data; private int topOfStack; private static final int INITIAL-CAPACITY-5; Add a constructor that will initialize the stack with a user-defined initial capacity that must be greater than 0. If the parameter is 0 or less, throw an IllegalArgumentException with the comment "Array initial size error". The top of the stack is assigned a value of-1 to indicate that the stack is currently empty public ArrayBasedStack(int capacity) if(capacity throw new IllegaLArgumentException"Array initial size error." SuppressWarningsC"unchecked" TC] tempStack-CT new 0bject[capacity] data-tempStack; topOfStack-1; Another constructor takes no parameters and sets the capacity of the data array to the default value. Add a private method called expandArray that will double the size of the array when called. To reduce the amount of code, you can use the copyOf method in java.util.Arrays Add another private method called is ArrayFullO that returns true if the topOfStack exceeds the largest index value in the array Stacklnterface method implementations Add a method called push that will add an element to the top of the stack If the array is full, increase the length of the array by calling the private method, expandArrayO that you wrote to expand the array Increase the top0fStack to point to the new index location and insert the new element into the stack The isEmptyO method returns true if there are no elements in the stack and false otherwise. If the top of the stack is less than 0 then there are no elements currently in the stack. Implement isEmptyO The pop method will remove the last element that was added to the stack. Implement popOby following these guidelines If stack is empty, throw an EmptyStackException. If there are elements in the stack, get the element at the top of the stack (index is topOfstack) and decrement the index of the top of the stack. Set the previous top of the stack to null. Decrement the index of the top of the stack and return the previous top. Add a method called peek() that will return the top of the stack but will not remove it. If the stack is empty, throw an exception. Otherwise, return the element at the top of the stack

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!