Question
20. Show what is written by the following segments of code, given that item1, item2, and item3 are int variables, and s is an object
20. Show what is written by the following segments of code, given that item1, item2, and item3 are int variables, and s is an object that its the abstract description of a stack as given in the section. Assume that you can store and retrieve variables of type int on s.
a.
item1 = 1; item2 = 0; item3 = 4;
s.push(item2); s.push(item1); s.push(item1 + item3);
item2 = s.top();
s.push (item3*item3); s.push(item2); s.push(3);
item1 = s.top();
s.pop();
System.out.println(item1 + " " + item2 + " " + item3);
while (!s.isEmpty())
{
item1 = s.top(); s.pop();
System.out.println(item1);
}
b.
item1 = 4; item3 = 0; item2 = item1 + 1;
s.push(item2); s.push(item2 + 1); s.push(item1);
item2 = s.top(); s.pop();
item1 = item2 + 1;
s.push(item1); s.push(item3);
while (!s.isEmpty())
{
item3 = s.top(); s.pop();
System.out.println(item3);
}
System.out.println(item1 + " " + item2 + " " + item3);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started