Java Reference
In-Depth Information
E
XERCISES
1.
If you push the objects
x
,
y
, and
z
onto an initially empty stack, in what order will three
pop
operations remove
them from the stack?
2.
What pseudocode statements create a stack of the three strings
"Jill"
,
"Jane"
, and
"Joe"
, in that order with
"Jill"
at the top?
3.
Suppose that
s
and
t
are empty stacks and
a
,
b
,
c
, and
d
are objects. What do the stacks contain after the following
sequence of operations executes?
s.push(a);
s.push(b);
s.push(c);
t.push(d);
t.push(s.pop());
t.push(s.peek());
s.push(t.pop());
t.pop();
4.
What are the contents of the stack
pile
after the following statements execute? Assume that
MyStack
is a class
that implements the interface
StackInterface
.
StackInterface<String> pile =
new
MyStack<String>();
pile.push("Jane");
pile.push("Jess");
pile.push("Jill");
pile.push(pile.pop());
pile.push(pile.peek());
pile.push("Jim");
String name = pile.pop();
pile.push(pile.peek());
5.
Consider the following Java statements, assuming that
MyStack
is a class that implements the interface
StackInterface
:
int
n = 4;
StackInterface<Integer> stack =
new
MyStack<Integer>();
while
(n > 0)
{
stack.push(n);
n--;
}
// end while
int
result = 1;
while
(!stack.isEmpty())
{
int
integer = stack.pop();
result = result * integer;
}
// end while
System.out.println("result = " + result);
a.
What value is displayed when this code executes?
b.
What mathematical function does the code evaluate?