public class StackTest2
1: // Fig. 18.11: StackTest2.java
2: // Stack generic class test program.
3:
4: public class StackTest2
5: {
6: private Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
7: private Integer[] integerElements =
8: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
9:
10: private Stack< Double > doubleStack; // stack stores Double objects
11: private Stack< Integer > integerStack; // stack stores Integer objects
12:
13: // test Stack objects
14: public void testStacks()
15: {
16: doubleStack = new Stack< Double >( 5 ); // Stack of Doubles
17: integerStack = new Stack< Integer >( 10 ); // Stack of Integers
18:
19: testPush( "doubleStack", doubleStack, doubleElements );
20: testPop( "doubleStack", doubleStack );
21: testPush( "integerStack", integerStack, integerElements );
22: testPop( "integerStack", integerStack );
23: } // end method testStacks
24:
25: // generic method testPush pushes elements onto a Stack
26: public < T > void testPush( String name, Stack< T > stack,
27: T[] elements )
28: {
29: // push elements onto stack
30: try
31: {
32: System.out.printf( "\nPushing elements onto %s\n", name );
33:
34: // push elements onto Stack
35: for ( T element : elements )
36: {
37: System.out.printf( "%s ", element );
38: stack.push( element ); // push element onto stack
39: }
40: } // end try
41: catch ( FullStackException fullStackException )
42: {
43: System.out.println();
44: fullStackException.printStackTrace();
45: } // end catch FullStackException
46: } // end method testPush
47:
48: // generic method testPop pops elements from a Stack
49: public < T > void testPop( String name, Stack< T > stack )
50: {
51: // pop elements from stack
52: try
53: {
54: System.out.printf( "\nPopping elements from %s\n", name );
55: T popValue; // store element removed from stack
56:
57: // remove all elements from Stack
58: while ( true )
59: {
60: popValue = stack.pop(); // pop from stack
61: System.out.printf( "%s ", popValue );
62: } // end while
63: } // end try
64: catch( EmptyStackException emptyStackException )
65: {
66: System.out.println();
67: emptyStackException.printStackTrace();
68: } // end catch EmptyStackException
69: } // end method testPop
70:
71: public static void main( String args[] )
72: {
73: StackTest2 application = new StackTest2();
74: application.testStacks();
75: } // end main
76: } // end class StackTest2
77:
78: /**************************************************************************
79: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
80: * Pearson Education, Inc. All Rights Reserved. *
81: * *
82: * DISCLAIMER: The authors and publisher of this book have used their *
83: * best efforts in preparing the book. These efforts include the *
84: * development, research, and testing of the theories and programs *
85: * to determine their effectiveness. The authors and publisher make *
86: * no warranty of any kind, expressed or implied, with regard to these *
87: * programs or to the documentation contained in these books. The authors *
88: * and publisher shall not be liable in any event for incidental or *
89: * consequential damages in connection with, or arising out of, the *
90: * furnishing, performance, or use of these programs. *
91: *************************************************************************/