Source of StackTest.java


  1: // Fig. 19.16: StackTest.java
  2: // Program to test java.util.Stack.
  3: import java.util.Stack;
  4: import java.util.EmptyStackException;
  5: 
  6: public class StackTest 
  7: {
  8:    public StackTest()
  9:    {
 10:       Stack< Number > stack = new Stack< Number >();  
 11: 
 12:       // create numbers to store in the stack
 13:       Long longNumber = 12L;
 14:       Integer intNumber = 34567;
 15:       Float floatNumber = 1.0F;
 16:       Double doubleNumber = 1234.5678;
 17: 
 18:       // use push method
 19:       stack.push( longNumber ); // push a long
 20:       printStack( stack );
 21:       stack.push( intNumber ); // push an int
 22:       printStack( stack );
 23:       stack.push( floatNumber ); // push a float
 24:       printStack( stack );
 25:       stack.push( doubleNumber ); // push a double
 26:       printStack( stack );
 27: 
 28:       // remove items from stack
 29:       try 
 30:       {
 31:          Number removedObject = null;
 32: 
 33:          // pop elements from stack
 34:          while ( true ) 
 35:          {
 36:             removedObject = stack.pop(); // use pop method
 37:             System.out.printf( "%s popped\n", removedObject );
 38:             printStack( stack );
 39:          } // end while
 40:       } // end try
 41:       catch ( EmptyStackException emptyStackException ) 
 42:       {
 43:          emptyStackException.printStackTrace();
 44:       } // end catch
 45:    } // end StackTest constructor
 46:    
 47:    private void printStack( Stack< Number > stack )
 48:    {
 49:       if ( stack.isEmpty() )
 50:          System.out.print( "stack is empty\n\n" ); // the stack is empty
 51:       else  // stack is not empty
 52:       {
 53:          System.out.print( "stack contains: " );      
 54:          
 55:          // iterate through the elements
 56:          for ( Number number : stack )
 57:             System.out.printf( "%s ", number );
 58:       
 59:          System.out.print( "(top) \n\n" ); // indicates top of the stack
 60:       } // end else
 61:    } // end method printStack
 62:    
 63:    public static void main( String args[] )
 64:    {
 65:       new StackTest();
 66:    } // end main
 67: } // end class StackTest
 68: 
 69: 
 70: /**************************************************************************
 71:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 72:  * Pearson Education, Inc. All Rights Reserved.                           *
 73:  *                                                                        *
 74:  * DISCLAIMER: The authors and publisher of this book have used their     *
 75:  * best efforts in preparing the book. These efforts include the          *
 76:  * development, research, and testing of the theories and programs        *
 77:  * to determine their effectiveness. The authors and publisher make       *
 78:  * no warranty of any kind, expressed or implied, with regard to these    *
 79:  * programs or to the documentation contained in these books. The authors *
 80:  * and publisher shall not be liable in any event for incidental or       *
 81:  * consequential damages in connection with, or arising out of, the       *
 82:  * furnishing, performance, or use of these programs.                     *
 83:  *************************************************************************/