Question
Postfix Accumulator (Using a Stack) Summary Create a class ( PostfixAccumulator ) that implements the AccumulatorInterface and allows the program PostfixCalculator.java to evaluate postfix expressions.
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.
Details
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, 10 5 + 3 *. Our postfix accumulator completes each operation as it is entered. For the expression above:
The 10 gets entered. The accumulator remembers and returns the 10 (but our program ignores this return value).
The 5 gets entered. The accumulator remembers and returns the 5 (which, again, our program ignores).
The + gets entered. The accumulator adds the 10 and the 5 to get and return 15 (ignored).
The 3 is entered. The accumulator remembers and returns the 3 (ignored).
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!
__________________________________________________________________________________________________________________________________
AccumulatorInterface.java:
/** * An interface for an object that accepts operators and numbers (integer only) * and returns a value based on what operators and values it has accepted. * * @author Mark Young (A00000000) */ public interface AccumulatorInterface { /** * Enter an integer operand into the accumulator. * Return value is implementation-dependent, * but is typically the latest value of the accumulator. * * @param n the number to enter. * @return implementation dependent. */ public int enterNumber(int n); /** * Enter an operator into the accumulator. * Return value is implementation-dependent, * but is typically the latest value of the accumulator. * * @param op the operator to enter. * @return implementation dependent. */ public int enterOperator(char op); /** * Get the value from the accumulator. * * @return the value currently saved in the accumulator. */ public int getValue(); /** * Return the accumulator to its original state. */ public void reset(); }
__________________________________________________________________________________________________________________________________
PostfixCalculator.java:
import java.util.Scanner; /** * A program that runs a postfix calculator using an object that implements the * AccumulatorInterface. * * @author Mark Young (A00000000) */ public class PostfixCalculator { /** * @param args the command line arguments */ public static void main(String[] args) { // create required objects Scanner kbd = new Scanner(System.in); AccumulatorInterface pfc = new PostfixAccumulator(); // create necessary variables String line; Scanner input; // introduce yourself printIntroduction(); // loop until a blank line System.out.print(" Enter formula > "); line = kbd.nextLine(); while (!line.equals("")) { try { input = new Scanner(line); // read number/operator until line ends while (input.hasNext()) { if (input.hasNextInt()) { pfc.enterNumber(input.nextInt()); } else { pfc.enterOperator(input.next().charAt(0)); } } System.out.println(line); System.out.println(" = " + pfc.getValue()); } catch (IllegalStateException|ArithmeticException ise) { System.out.println("Error: " + ise.getMessage()); } // reset the accumulator to get rid of old values or garbage pfc.reset(); // get next math expression System.out.print(" Enter formula > "); line = kbd.nextLine(); } System.out.println(" " + "Goodbye! "); } /** * Print the introduction to this program. */ private static void printIntroduction() { System.out.println(" " + "Line-Oriented Postfix Calculator " + "-------------------------------- " + " " + "This calculator evaluates postfix expressions. " + "Enter a postfix expression on each line, " + "and the calculator will tell you its value. " + "For example, enter the line" + " 10 5 + 3 * " + "and the program will tell you it's 45. " + " " + "You can use only integer numbers, " + "and the +, -, * and / operators. " + " " + "Enter your math expression at the prompt. " + "Just leave the input line blank when " + "you're done. " + ""); } }
__________________________________________________________________________________________________________________________________
OUTPUT:
(BOLD FONT REFERS TO USERS INPUT)
Line-Oriented Postfix Calculator
------------------------------------------
This calculator evaluates postfix expressions. Enter a postfix expression on each line, and the calculator will tell you its value.
For example, enter the line
10 5 + 3 *
and the program will tell you it's 45.
(You can use only integer numbers, and the +, -, * and / operators.
Enter your math expression at the prompt. Just leave the input line blank when you're done.
Enter formula > 1 1 +
1 1 +
= 2
Enter formula > 2 3 *
2 3 *
= 6
Enter formula > 40 5 -
40 5 -
= 35
Enter formula > 100 5 /
100 5 /
= 20
Enter formula > 1 2 3 4 5 + + + +
1 2 3 4 5 + + + +
= 15
Enter formula > 1 2 3 4 5 * * * *
1 2 3 4 5 * * * *
= 120
Enter formula > 10 5 + 3 *
10 5 + 3 *
= 45
Enter formula > 100 100 * 20 20 * -
100 100 * 20 20 * -
= 9600
Enter formula > 100 100 *
100 100 *
= 10000
Enter formula > 20 20 *
20 20 *
= 400
Enter formula > 10000 400 -
10000 400 -
= 9600
Enter formula > 100 100 * 20 - 20 *
100 100 * 20 - 20 *
= 199600
Enter formula > 100
100
= 100
Enter formula > +
Error: Not enuf operands for +
Enter formula > 100 100
100 100
Error: Too many numbers in expression
Enter formula > 100 100 + +
Error: Not enuf operands for +
Enter formula > 20 0 /
Error: / by zero
Enter formula > 5 10 +
5 10 +
= 15
Enter formula >
Goodbye!
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