The PostfixEvaluator class uses the java.util.Stack class to create the stack attribute. The java.util.Stack class is one of two stack implementations provided by the Java Collections API. We revisit the other implementation, the Deque interface, in Chapter 13. The evaluate method performs the evaluation algorithm described earlier, supported by the isOperator and evalSingleOp methods. Note that in the evaluate method, only operands are pushed onto the stack. Operators are used as they are encountered and are never put on the stack. This is consistent with the evaluation algorithm we discussed. An operand is put on the stack as an Integer object, instead of as an int primitive value, because the stack collection is designed to store objects. When an operator is encountered, the two most recent operands are popped off the stack. As mentioned above, the first operand popped is actually the second operand in the expression and that the second operand popped is the first operand in the expression. This order doesn’t matter in the cases of addition and multiplication, but it certainly matters for subtraction and division.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

The PostfixEvaluator class uses the java.util.Stack class to create the
stack attribute. The java.util.Stack class is one of two stack implementations
provided by the Java Collections API. We revisit the other implementation, the
Deque interface, in Chapter 13.
The evaluate method performs the evaluation algorithm described earlier, supported by the isOperator and evalSingleOp methods. Note that in the evaluate
method, only operands are pushed onto the stack. Operators are used as they are
encountered and are never put on the stack. This is consistent with the evaluation algorithm we discussed. An operand is put on the stack as an Integer object, instead
of as an int primitive value, because the stack collection is designed to store objects.
When an operator is encountered, the two most recent operands are popped
off the stack. As mentioned above, the first operand popped is actually the second
operand in the expression and that the second operand popped is the first operand
in the expression. This order doesn’t matter in the cases of addition and multiplication, but it certainly matters for subtraction and division.

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning