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
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 stackStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started