// Stack class of package java.util. import java.util.Stack; import java.util.EmptyStackException; public class StackTest { public static void main(String[] args) { tack stack = new Stack<>(); // create a Stack // use push method stack.push(12L); 14 System.out.println("Pushed 12L"); 15 printStack(stack); 16 stack.push(34567); 17 System.out.println("Pushed 34567"); 18 printStack(stack); 19stack.push(1.0F); 20 System.out.println("Pushed 1.0F"); 21 printStack(stack); 22 stack.push(1234.5678); // 23 System.out.println("Pushed 1234.5678 "); 24 printStack(stack); 25 26 // remove items from stack 27 try 28 { 29 Number removedObject = null; 30 31 // pop elements from stack 32 while (true) 33 { 34 removedObject = stack.pop(); // use pop method 35 System.out.printf("Popped %s%n", removedObject); 36 printStack(stack); 37 } 38 } 39 catch (EmptyStackException emptyStackException) 40 { emptyStackException.printStackTrace(); 42 } 43 } 44 45 // display Stack contents 46 private static void printStack(Stack stack) 47 { 48 if (stack.isEmpty() ) 49 System.out.printf("stack is empty%n%n"); // the stack is empty 50 else // stack is not empty 51 System.out.printf("stack contains: %s (top)%n", stack); 52 } 53 } // end class StackTest