In: Computer Science
Java Question:
COSC 2436 Lab 4 (Submit your Word file with answers)
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();
StackInterface pile = new MyStack<>();
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());
Before going into explanation and answers, please understand the
following stack operations in Java:
A. push : Adds the contents to the top of stack.
B. pop: Removes and return the top most element from the
stack.
C. peek : Fetches or return the top most element but not removes it
from the stack.
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?
Answer- The order of removal after the three pop operations will be z,y and x.
Explanations- The objects entering the stack will be in the order of x at first then y and at last z. so when the objects are removed the last (i.e) z will come out first, then y and at last x will be removed.
2) What pseudocode statements create a stack of the three strings "Jill", "Jane", and "Joe", in that order with "Jill" at the top?
Answer- pseudocode statements ->
Stack<String> s = new Stack<String>();
s.push("Jane"); or s.push("Joe");
s.push("Joe"); or s.push("Jane");
s.push("Jill")
That is to keep Jill on top we have to insert or push jill in the stack at last.
Explanations- In stack, the object that is pushed first into the stack is at the last in the stack and the object that is pushed last into the stack sits at the top of the stack (i.e) jill is pushed into the stack at last so that jill sits on the top of the stack.
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();
Answer - The contents of stack s after given operations will be [a,b,b] .
The contents of stack t after given operations will be [d].
Explanations- In the stack s objects are pushed in order as follows at first a, b, c and in the stack t only one object d is pushed at first. After that we remove (c) from stack s and insert (c) in the stack t with the statement {t.push(s.pop())}.After that the top value (b) from stack s is taken and pushed into stack t with the statement{t.push(s.peek())}. After that, from stack t the top value (b) is removed and pushed into stack s with the statement{s.push(t.pop())} and at last from the stack t the top element is removed which is (c).So, at last stack s contents are [a,b,b] and stack t contents are [d].
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 pile = new MyStack<>();
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());
Answers- The contents of stack pile after given operations will be [Jane, Jess, Jill, Jill, Jill].
Explanations- At first, [Jane,Jess,Jill] is pushed into the stack. After that jill is popped and pushed in the stack so we have [Jane,Jess,Jill] in the stack. After this top element Jill is taken and pushed back into the stack so now we have [Jane,Jess,Jill,Jill] in the stack. Now Jim is pushed into the stack so now we have [Jane,Jess,Jill,Jill,Jim] in the stack. Now again pop is called and Jim is removed from the stack so now we have [Jane,Jess,Jill,Jill] in the stack. At last again the top element Jill is taken and pushed back into the stack so now we have [Jane,Jess,Jill,Jill,Jill] in the stack. So the final contents of the pile stack after given operations will be [Jane, Jess, Jill, Jill, Jill].