Question: Stack Interface: /* * Starter and/or reference code */ public interface Stack { /** Adds a new entry to the top of this stack. @param


Stack Interface:
| /* * Starter and/or reference code */ public interface Stack /** Adds a new entry to the top of this stack. @param item An object to be added to the stack. @throws IllegalStateException - if the element cannot be added at this time due to capacity restrictions */ public void push (T item) throws IllegalStateException ;
/** Removes and returns this stack's top entry. @return The object at the top of the stack. @return null if the stack is empty */ public T pop();
/** Retrieves this stack's top entry. @return The object at the top of the stack. @return null if the stack is empty */ public T peek();
/** Detects whether this stack is empty. @return True if the stack is empty. */ public boolean isEmpty();
/** Retrieves the number of entries in this stack. * @return number of entries. */ public int length(); }
SAMPLE OUTPUT:
|



Implement the stack ADT following the interface provided. Then, design and implement a driver class that tests all the stack operations. This test should check all edge/boundary cases, such as, what happens when you pop from an empty stack? What happens when you push to a full stack What happens if you try to use the toString method in a variety of situations? Lastly, create a menu driven application that tests the push, pop, and peek methods using integers. To do: - Implement the stack ADT using an array to store the data. Implement the push(), pop(), peek(), isEmpty(), and length() methods as specified in the given interface below - Design and implement the test class according to the specifications provided. - At minimum, the implementation must implement the provided interface and also override the toString method. - When implementing the stack, you cannot actually create an array of generic items. In other words, you cannot create an array of type T. Intuitively, you would try to do something like: T[ ] stack = new T[100]; That line of code will result in a "T cannot be resolved to a type error." Instead, you have to create an array of Object items and cast it to the generic type T T[ ] stack = (T[ ]) new Object [100]; Checking if it's empty. Current contents: Is it empty? true Displaying contents of an empty stack: \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# 1: View all contents. 2: Pop 3: Peek 4: Push 5: Exit program. Checking if it's empty. Current contents: Is it empty? true Adding 5 items (integers) Current contents: 012234 Trying to add to a stack that is filled to capacity. Cannot add, because push resulted in IllegalstateException Popping one item and displaying contents of stack. Popped item: 4 Peeking one item and displaying contents of stack. Peeked item: 3 Current contents: 0123 Popping one item and displaying contents of stack. Popped item: 3 Current contents: 012 Checking if it's empty. Current contents: 012 Is it empty? false Emptying all contents: Popping one item and displaying contents of stack. Popped item: 2 Current contents: 01 Popping one item and displaying contents of stack. Popped item: 1 Current contents: 0 Popping one item and displaying contents of stack. Popped item: 0 Current contents: Checking if it's empty. Current contents: Is it empty? true
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

