Source of StackTest.java


  1: // Fig. 18.10: StackTest.java
  2: // Stack generic class test program.
  3: 
  4: public class StackTest 
  5: {
  6:    private double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
  7:    private int[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  8: 
  9:    private Stack< Double > doubleStack; // stack stores Double objects
 10:    private Stack< Integer > integerStack; // stack stores Integer objects
 11: 
 12:    // test Stack objects
 13:    public void testStacks()
 14:    {
 15:       doubleStack = new Stack< Double >( 5 ); // Stack of Doubles
 16:       integerStack = new Stack< Integer >( 10 ); // Stack of Integers
 17: 
 18:       testPushDouble(); // push double onto doubleStack
 19:       testPopDouble(); // pop from doubleStack
 20:       testPushInteger(); // push int onto intStack
 21:       testPopInteger(); // pop from intStack
 22:    } // end method testStacks
 23: 
 24:    // test push method with double stack
 25:    public void testPushDouble()
 26:    {
 27:       // push elements onto stack 
 28:       try
 29:       {
 30:          System.out.println( "\nPushing elements onto doubleStack" );
 31: 
 32:          // push elements to Stack
 33:          for ( double element : doubleElements )
 34:          {
 35:             System.out.printf( "%.1f ", element );
 36:             doubleStack.push( element ); // push onto doubleStack
 37:          } // end for
 38:       } // end try
 39:       catch ( FullStackException fullStackException )
 40:       {
 41:          System.err.println();
 42:          fullStackException.printStackTrace();
 43:       } // end catch FullStackException
 44:    } // end method testPushDouble
 45: 
 46:    // test pop method with double stack
 47:    public void testPopDouble()
 48:    {
 49:       // pop elements from stack
 50:       try
 51:       {
 52:          System.out.println( "\nPopping elements from doubleStack" );
 53:          double popValue; // store element removed from stack
 54: 
 55:          // remove all elements from Stack
 56:          while ( true )
 57:          {
 58:             popValue = doubleStack.pop(); // pop from doubleStack
 59:             System.out.printf( "%.1f ", popValue ); 
 60:          } // end while
 61:       } // end try
 62:       catch( EmptyStackException emptyStackException )
 63:       {
 64:          System.err.println();
 65:          emptyStackException.printStackTrace();
 66:       } // end catch EmptyStackException
 67:    } // end method testPopDouble
 68: 
 69:    // test push method with integer stack
 70:    public void testPushInteger()
 71:    {
 72:       // push elements to stack 
 73:       try
 74:       {
 75:          System.out.println( "\nPushing elements onto integerStack" );
 76: 
 77:          // push elements to Stack
 78:          for ( int element : integerElements )
 79:          {
 80:             System.out.printf( "%d ", element );
 81:             integerStack.push( element ); // push onto integerStack
 82:          } // end for
 83:       } // end try
 84:       catch ( FullStackException fullStackException )
 85:       {
 86:          System.err.println();
 87:          fullStackException.printStackTrace();
 88:       } // end catch FullStackException
 89:    } // end method testPushInteger
 90: 
 91:    // test pop method with integer stack
 92:    public void testPopInteger()
 93:    {
 94:       // pop elements from stack
 95:       try
 96:       {
 97:          System.out.println( "\nPopping elements from integerStack" );
 98:          int popValue; // store element removed from stack
 99: 
100:          // remove all elements from Stack
101:          while ( true )
102:          {
103:             popValue = integerStack.pop(); // pop from intStack
104:             System.out.printf( "%d ", popValue );
105:          } // end while
106:       } // end try
107:       catch( EmptyStackException emptyStackException )
108:       {
109:          System.err.println();
110:          emptyStackException.printStackTrace();
111:       } // end catch EmptyStackException
112:    } // end method testPopInteger
113: 
114:    public static void main( String args[] ) 
115:    {
116:       StackTest application = new StackTest();
117:       application.testStacks();
118:    } // end main
119: } // end class StackTest
120: 
121: /**************************************************************************
122:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
123:  * Pearson Education, Inc. All Rights Reserved.                           *
124:  *                                                                        *
125:  * DISCLAIMER: The authors and publisher of this book have used their     *
126:  * best efforts in preparing the book. These efforts include the          *
127:  * development, research, and testing of the theories and programs        *
128:  * to determine their effectiveness. The authors and publisher make       *
129:  * no warranty of any kind, expressed or implied, with regard to these    *
130:  * programs or to the documentation contained in these books. The authors *
131:  * and publisher shall not be liable in any event for incidental or       *
132:  * consequential damages in connection with, or arising out of, the       *
133:  * furnishing, performance, or use of these programs.                     *
134:  *************************************************************************/