SHow the effect of each of the following operations on stack s. Assume that y(ty
ID: 3623948 • Letter: S
Question
SHow the effect of each of the following operations on stack s. Assume that y(type Character) constains the character '&'. What are the final values of x and success and the contents of the stack s?CODE:
Stack<Character> s = new Stack<Character>();
char x;
s.push('+');
try{
x = s.pop();
success = true;
}
catch (EmptyStackException e){
success = false;
}
try{
x = s.pop();
success = true;
}
catch (EmptyStackException e){
success = false;
}
s.push('(');
s.push(y);
try {
x = s.pop();
success = true;
}
catch (EmptyStackException e){
success = false;
}
Explanation / Answer
Stack<Character> s = new Stack<Character>();
char x;
s.push('+');
try{
x = s.pop(); // here x will be '+'
success = true; // ssuccess will be true
}
catch (EmptyStackException e){ // no error to catch
success = false;
}
try{
x = s.pop(); // error stack is empty
success = true;
}
catch (EmptyStackException e){ // this will execute
success = false; // success will be false
}
s.push('(');
s.push(y);
try {
x = s.pop(); // x = '&'
success = true; // success will be true
}
catch (EmptyStackException e){
success = false;
}
// final valus of x is '&'
// successs = true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.