Write a postfixcalculator class - create multiple


Write a PostfixCalculator class.

1. You should have two private member variables in this class
- A stack of double operand
- A map of strings to operator objects. (something like
private MapoperatorMap;)

2. The constructor should fill the operator map with associations of symbols to operator objects as described in the table below. You will have to create multiple implementations of the operator interface in order to do this.
- The Operator implementations should be nested inside the PostfixCalculator class. It is up to you if you make them static nested classes, non-static inner classes, local classes, or anonymous classes, but they must all be inside PostfixCalculator somehow.

3. The storeOperand method takes a double and pushes it onto the operand stack. It does not return anything.

4. The evalOperator method takes an operator string, looks up the corresponding operator object in the operator map, pops the appropriate number of operands (as given by the numArgs method) and places them into a list, evaluates the operator with the operands in the list, and pushes the result onto the operand stack. It does not return anything, but operators may have side effects, such as printing the argument to standard output.

Operators
Your calculator should recognize the following operators. Note that there can be more than one symbol for a given operation. (For example, 1 2 + would have the same result as 1 2 add)

Name

Symbol

# Arguments

Result

addition

+ add

2

Sum of arguments A B + → A+B

subtraction

- sub

2

Difference of arguments A B - → A-B

multiplication

* mult

2

Product of arguments

division

/ div

2

Quotient of arguments

print

= print

1

Print argument to standard out and return argument

Test Input
1 =
2 print
3 2 + =
3 2 add =
3 2 - =
3 2 sub =
3 2 * =
3 2 mult =
3 2 / =
3 2 div =
1 2 3 * + 4 - =
5 1 2 + 4 * + 3 - =
1 2 + 3 * 6 + 2 3 + / =
10 = 12 = / = 35 = + = 16 = - = 23 = * = 37 = + =

Test output
1.0
2.0
5.0
5.0
1.0
1.0
6.0
6.0
1.5
1.5
3.0
14.0
3.0
10.0
12.0
0.8333333333333334
35.0
35.833333333333336
16.0
19.833333333333336
23.0
456.16666666666674
37.0
493.16666666666674

Attachment:- Java code.rar

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write a postfixcalculator class - create multiple
Reference No:- TGS02384498

Now Priced at $30 (50% Discount)

Recommended (96%)

Rated (4.8/5)