Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please code in Java for postfixAccummulator.java Postfix Accumulator (Using a Stack) Summary Create a class (PostfixAccumulator) that implements the AccumulatorInterface and allows the program PostfixCalculator.java
please code in Java for postfixAccummulator.java
Postfix Accumulator (Using a Stack) Summary Create a class (PostfixAccumulator) that implements the AccumulatorInterface and allows the program PostfixCalculator.java to evaluate postfix expressions. Use a Deque in your implementation. Use only the Stack operations of the Deque. I have added a program called DequeAsStack to show you how to use a Deque as a stack. It shows you the four stack operations that you are allowed to use: push, isEmpty, pop, and peek. Detals I have provided you with a program (PostfixCalculator) that is supposed to let the user enter a postfix expression and report back the value of that expression. Unfortunately, it's missing a class: PostfixAccumulator. This class allows us to enter numbers and operations in the postfix (i.e. operator after operands) order. It implements the AccumulatorInterface, (also seen in A01) which has four methods: - int enterNumber(int n ) which enters a number into the accumulator. - int enterOperator(char op) which enters an operator into the accumulator. - int getValue( ) which gets the current value of the accumulator. - void reset() which returns the accumulator to its initial state in preparation for a new calculation. Our program gives the PostfixAccumulator object numbers and operators in the order the user entered them. For example, 105+3 *. Our postfix accumulator completes each operation as it is entered. For the expression above: 1. The 10 gets entered. The accumulator remembers and returns the 10 (but our program ignores this return value). 2. The 5 gets entered. The accumulator remembers and returns the 5 (which, again, our program ignores). 3. The + gets entered. The accumulator adds the 10 and the 5 to get and return 15 (ignored). Our program gives the PostfixAccumulator object numbers and operators in the order the user entered them. For example, 105+3 *. Our postfix accumulator completes each operation as it is entered. For the expression above: 1. The 10 gets entered. The accumulator remembers and returns the 10 (but our program ignores this return value). 2. The 5 gets entered. The accumulator remembers and returns the 5 (which, again, our program ignores). 3. The + gets entered. The accumulator adds the 10 and the 5 to get and return 15 (ignored). 4. The 3 is entered. The accumulator remembers and returns the 3 (ignored). 5. The "is entered. The accumulator multiplies the 15 by the 3 to get and return 45 (also ignored). Our PostfixAccululator only needs to recognize the four operations +, and / (plus, minus, times and divide), Other operations can be ignored (as stated in the AccumulatorInterface specification). Notes The PostfixAccumulator will throw exceptions as appropriate. - An IllegalStateException gets thrown when: - An operator is entered but there are not enuf operands available for it to be evaluated. - The value is requested, but there is more than one item on the stack. - The value is requested, but there are no items on the stack. In each case, the exception has a message indicating what the problem was. (The program prints that message, not the accumulator.) - An ArithmeticException is thrown if the accumulator divides a number by 0 . NOTE: this happens automatically whenever java divides a number by zero. The program catches this exception and deals with it correctly. It's not your problem! Don't forget to make your code fit the style guidelines for the course. iport java.util. Seanner; A program that runs a postfix calculator using an object that implements the AccumulatorInterface. (1) blic class Postfixcaleulator \& ) Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started