Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a driver program in which main has a GenericStack variable (you MUST use t

ID: 3816827 • Letter: W

Question

Write a driver program in which main has a GenericStack variable (you MUST use the GenericStack class shown on page 1 of Lesson 1). Instantiate a GenericStack and assign to the main variable. Add several Strings (at least 3, your choice of Strings) to the GenericStack (use the push method). Display the contents using the GenericStack's toString method. Then use a while loop (loops while the GenericStack isn't empty) that removes an element using the pop method, and displays it.

Make sure you DON'T change the GenericStack class written above. Upload only main.

GenericStack class:

Explanation / Answer


public class GenericStackTest {
public static void main(String[] args)
{
GenericStack<String> genericStack = new GenericStack<>();
  
genericStack.push("String1");
genericStack.push("String2");
genericStack.push("String3");
genericStack.push("String4");
  
System.out.println("Content of stack: ");
System.out.println(genericStack.toString());
  
while(!genericStack.isEmpty())
{
System.out.println("Removed " + genericStack.pop());
}
  
}
}

Sample execution

Content of stack:
stack: [String1, String2, String3, String4]
Removed String4
Removed String3
Removed String2
Removed String1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote