Question: Application #1 Using ArrayDeque class from java.util write a program that implements a special stack that has a finite size but allows an unlimited number
Application #1
Using ArrayDeque class from java.util write a program that implements a special stack that has a finite size but allows an unlimited number of push operations. If the stack is full when a push occurs, the stack makes room for the new entry by deleting the entry at its bottom. A browser that maintains a limited history could use this kind of stack. Implement this stack by using a deque data structure implemented by ArrayDeque class.
Please note that capacity should be declared as final, DEFAULT_CAPACITY should be set to 20. To add, remove, and retrieve elements utilize ArrayDeque methods: push, pop, peek, removeLast.
UML DIAGRAM:
Please note two methods added for testing: main (see the sample run) and display.

Implement all methods defined in the UML Diagram.
public class DropoutStack{ // TODO Project 1 // IMPLEMENT ALL THE METHODS DEFINED IN THE UML DIAGRAM // push method should utilize removeLast() // UNCOMMENT THE display and main METHODS WHEN READY FOR TESTING /** * METHOD display implemented for debugging purposes */ public void display() { if (isEmpty()) System.out.println("The stack is empty"); else System.out.println(Arrays.toString(this.stack.toArray())); } public static void main(String args[]) { System.out.println("************** TESTING DROPOUT STACK ************** "); DropoutStack dropoutStack = new DropoutStack (); System.out.println("----> Adding 20 items to empty stack of capacity of 20"); for (int i = 0; i out.println("----> The content of the stack is:"); dropoutStack.display(); System.out.println("----> The top of the stack is: " + dropoutStack.peek()); System.out.println(" ----> Adding 5 more items to full stack"); for (int i = 20; i out.println("push " + i); dropoutStack.push(i); } System.out.println("----> The content of the stack is:"); dropoutStack.display(); System.out.println("----> The top of the stack is: " + dropoutStack.peek()); System.out.println(" ----> Removing all elements from the stack:"); while (!dropoutStack.isEmpty()) { System.out.println("----> pop " + dropoutStack.pop()); } dropoutStack.display(); System.out.println("----> The top of the stack is: " + dropoutStack.peek()); System.out.println(" ----> Trying to pop from the empty stack"); System.out.println("----> Got back " + dropoutStack.pop()); System.out.println(" ----> Trying to peek at the top of the empty stack"); System.out.println("----> Got back " + dropoutStack.peek()); System.out.println(" ----> Adding 22 items to empty stack of capacity of 20"); for (int i = 0; i out.println("----> The content of the stack is:"); dropoutStack.display(); System.out.println(" ----> Clearing the stack with the clear method"); dropoutStack.clear(); System.out.println("----> The content of the stack is:"); dropoutStack.display(); } }
SAMPLE RUN:
************** TESTING DROPOUT STACK **************
----> Adding 20 items to empty stack of capacity of 20
----> The content of the stack is:
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
----> The top of the stack is: 19
----> Adding 5 more items to full stack
push 20
push 21
push 22
push 23
push 24
----> The content of the stack is:
[24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5]
----> The top of the stack is: 24
----> Removing all elements from the stack:
----> pop 24
----> pop 23
----> pop 22
----> pop 21
----> pop 20
----> pop 19
----> pop 18
----> pop 17
----> pop 16
----> pop 15
----> pop 14
----> pop 13
----> pop 12
----> pop 11
----> pop 10
----> pop 9
----> pop 8
----> pop 7
----> pop 6
----> pop 5
The stack is empty
----> The top of the stack is: null
----> Trying to pop from the empty stack
----> Got back null
----> Trying to peek at the top of the empty stack
----> Got back null
----> Adding 22 items to empty stack of capacity of 20
----> The content of the stack is:
[21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
----> Clearing the stack with the clear method
----> The content of the stack is:
The stack is empty
DropoutStack | e stack ArrayDeque
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
