Question: Please write code in Java. you will be implementing a text-based postfix calculator which takes input in postfix notation and displays the results of the
Please write code in Java. you will be implementing a text-based postfix calculator which takes input in postfix notation and displays the results of the computation.
The normal mathematical notation that we use is called "infix" notation, where the mathematical operator is in between the operands (e.g., 3 - 5). With postfix notation, the operator comes after the operands. For example, the expression 3 - 5 would be written as 3 5 -. As another example, the expression 52 - (5 + 7) * 4 would be written as:
52 5 7 + 4 * -
All of the classes should be inside the calculator package. The three classes you need to implement are outlined below:
- Operation: This class represents a mathematical operation. It should have just a single static method:
- public static int performOperation(char op, int left, int right): Perform the math operation on these two operands and return the result. Valid operations are +, -, * and /. If the input is not one of these four operations, it should throw an IllegalArgumentException. This method could throw an ArithmeticException if we try and divide by zero (Java will do this naturally for you).
- CalculatorMemory: The memory of the calculator. It must have the following methods:
- public void push(int number): Add the number to memory. Must run in O(1).
- public int pop(): Return and remove the most recently pushed value. Must run in O(1).
- public boolean isEmpty()
- public int size()
- public void clear(): Remove everything from the memory.
- public String toString(): A String version of the numbers stored. The String should look like the memory contents output below.
- Calculator: This is the class where we actually interact with the user and run everything. It needs to have the two public methods below and at least one private helper method (you pick the name, etc.) :
- A zero parameter constructor
- public void run(): This runs the calculator, which will prompt the user for numbers, do the calculations, etc.
Your program will prompt the user for either a number or an operator and will either add the number to the stack directly or perform the operation and then add the result back to the stack. Here is an example execution of the program following the example above (below picture)
Keep going until the user enters "quit" (without quotes). Support +, -, *, and /, as well as two other operators: (pop, clear) Handle all errors
- Divide by zero: "Error: divide by zero"
- Invalid input (not a number of operator): "Error: expected number or operator"
- Not enough operators: "Error: operator requires two arguments"
- Bad pop: "Error: pop requires one argument"
When an error occurs, the Stack should not be changed - only an error message is printed out.

Enter a number or operator: 52 Memory contents: 52 Enter a number or operator: 5 Memory contents: 5 52 Enter a number or operator: 7 Memory contents: 52 Enter a number or operator: + Answer: 12 Memory contents: 12 52 Enter a number or operator: 4 Memory contents: 4 12 Here is an example showing the errors: Enter a number or operator: pop Error: pop requires one argument Memory contents: Enter a number or operator: asdf Error: expected number or operator Memory contents: Enter a number or operator: 1 Memory contents: 1 Enter a number or operator: + Error: operator requires two arguments Memory contents: Enter a number or operator: 0 Memory contents: 0 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
