Complete the code to declare a stack of integers using Java's Stack class.
Stack<Integer> numbers = new [1]<>();We use Stack to create a stack data structure in Java.
Complete the code to add an element to the top of the stack.
numbers.[1](10);
add which works for lists but is not the stack method.offer which is for queues.The push method adds an element to the top of the stack.
Fix the error in the code to remove the top element from the stack.
int top = numbers.[1]();remove which requires an index or object.poll which is for queues.The pop method removes and returns the top element of the stack.
Fill both blanks to check if the stack is empty and print a message.
if (numbers.[1]()) { System.out.println("Stack is [2]."); }
empty() method which exists but returns a boolean differently.full() or clear() which are not correct here.The isEmpty() method checks if the stack has no elements, so the message says "empty".
Fill all three blanks to create a stack, push two elements, and pop one.
Stack<String> stack = new [1]<>(); stack.[2]("apple"); stack.[3]();
add instead of push.remove or delete instead of pop.We create a Stack, add an element with push, and remove the top element with pop.
