Learning Objectives Gain some experience using dynamic data structures, which can grow and shrink at execution time. In this lab, you will use Stacks , and Maps . Learn to iterate through a dynamic data structures Setup Set up your directory: $ mkdir -p ~/cs180/lab14 $ cd ~/cs180/lab14 $ drjava & Evaluator Introduction Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed. The description “Polish” refers to the nationality of logician Jan Łukasiewicz, who invented (prefix) Polish notation in the 1920s. In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4 . If there are multiple operations, the operator is given immediately after its second operand; so the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in RPN: 4 is first subtracted from 3, then 5 added to it. An advantage of RPN is that it obviates the need for parentheses that are required by infix notation. While 3 − 4 × 5 can also be written 3 − (4 × 5) , that means something quite different from (3 − 4) × 5 . In postfix, the former could be written 3 4 5 × − , which unambiguously means 3 (4 5 ×) − which reduces to 3 20 − ; the latter could be written 3 4 − 5 × (or 5 3 4 − × , if keeping similar formatting), which unambiguously means (3 4 −) 5 × . Despite the name, reverse Polish notation is not exactly the reverse of Polish notation, for the operands of non-commutative operations are still written in the conventional order (e.g. ÷ 6 3 in Polish notation and 6 3 ÷ in reverse Polish both evaluate to 2, whereas 3 6 ÷ ; in reverse Polish notation would evaluate to ½). Example The infix expression 5 + ((1 + 2) × 4) − 3 can be written down like this in RPN: 5 1 2 + 4 × + 3 − The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the list of values the algorithm is “keeping track of” after the Operation given in the middle column has taken place): Input Operation Stack Comment 5 Push value 5 - 1 Push value 1,5 - 2 Push value 2,1,5 - + Add 3,5 Pop two values (1, 2) and push result (3) 4 Push value 4,3,5 - x Multiply 12,5 Pop two values (3, 4) and push result (12) + Add 17 Pop two values (5, 12) and push result (17) 3 Push value 3,17 - - Subtract 14 Pop two values (17, 3) and push result (14) Result 14 - Your Task Now that we have explained the working of an RPN Calculator, it is time to make one ourselves. We will do that by using the Stack data structure, which is already implemented in the Java libraries. Use the push and pop functions as well as the constructor. Implement a class Evaluator with a static class method evaluate that takes a String argumen.