Question: Java problem. Please only answer if you understand everything. Thank you. There is a part a and part b for this one problem. Show what
Java problem. Please only answer if you understand everything. Thank you.
There is a part a and part b for this one problem.
Show what is written by the following segments of code, given that item1, item2, and item3 are int variables, and stack is an object that fits the abstract description of a stack as given in the section. Please explain how you get these answers.
Assume that you can store and retrieve variables of type int on stack.
a.
item1=1;
item2=0;
item3=4;
stack.push(item2);
stack.push(item1);
item2=stack.top();
stack.push(item3*item3);
stack.push(item2);
stack.push(3);
item1=stack.top();
stack.pop();
System.out.println(item1 + " " + item2 + " " + item3);
while (!stack.isEmpty())
{
item1=stack.top();
stack.pop();
System.out.println(item1);
}
b.
item1=4;
item3=0;
item2=item1 + 1;
stack.push(item2);
stack.push(item2 +1);
stacl.push(item1);
item2=stack.top();
stack.pop();
item1=item2 + 1;
stack.push(item1);
stack.push(item3);
while (!stack.isEmpty())
{
item3=stack.top();
stack.pop();
System.out.println(item3);
}
System.out.println(item1 + " " + item2 + " " + item3);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
