Java Reference
In-Depth Information
15
// push elements of doubleElements onto doubleStack
16
testPushDouble(doubleStack, doubleElements);
17
testPopDouble(doubleStack);
// pop from doubleStack
18
19
// push elements of integerElements onto integerStack
20
testPushInteger(integerStack, integerElements);
21
testPopInteger(integerStack);
// pop from integerStack
22
}
23
24
// test push method with double stack
25
private static void
testPushDouble(
26
Stack<Double> stack,
double
[] values)
27
{
28
System.out.printf(
"%nPushing elements onto doubleStack%n"
);
29
30
// push elements to Stack
31
for
(
double
value : values)
32
{
33
System.out.printf(
"%.1f "
, value);
34
stack.push(value);
// push onto doubleStack
35
}
36
}
37
38
// test pop method with double stack
39
private static void
testPopDouble(Stack<Double> stack)
40
{
41
// pop elements from stack
42
try
43
{
44
System.out.printf(
"%nPopping elements from doubleStack%n"
);
45
double
popValue;
// store element removed from stack
46
47
// remove all elements from Stack
48
while
(
true
)
49
{
50
popValue = stack.pop();
// pop from doubleStack
51
System.out.printf(
"%.1f "
, popValue);
52
}
53
}
54
catch
(EmptyStackException emptyStackException)
55
{
56
System.err.println();
57
emptyStackException.printStackTrace();
58
}
59
}
60
61
// test push method with integer stack
62
private static void
testPushInteger(
63
Stack<Integer> stack,
int
[] values)
64
{
65
System.out.printf(
"%nPushing elements onto integerStack%n"
);
66
Fig. 20.9
|
Stack
generic class test program. (Part 2 of 3.)