In this assignment, you will implement your code using a data structure.

Design and implement an application that evaluates a postfix expression that operates on integer operands using the arithmetic operators +, -, *, and /. We are already familiar with infix expressions, in which an operator is positioned between its two operands (e.g., 2+3). A postfix expression puts the operators after its operands (e.g., 2 3 +). Keep in mind that an operand could be the result of another operation. This eliminates the need for parentheses to force precedence. For example, the following infix expression:

(5 + 2) * (8 - 5)

is equivalent to the following postfix expression:

5 2 + 8 5 - *

The evaluation of a postfix expression is facilitated by using a stack. As you process a postfix expression from left to right, you encounter operands and operators. If you encounter an operand, push it on the stack. If you encounter an operator, pop two operands off the stack, perform the operation, and push the result back on the stack. When you have processed the entire expression, there will be one value on the stack, which is the result of the entire expression.

  • Plan ahead. Start by designing your application. Use a class to store the postfix. Plan out (not code) the instance data and methods using good object-oriented techniques: data encapsulation and information hiding.
  • Repeatedly ask the user for an input, show the answer (or exception message), and ask for another input, until a special input such as "exit" is entered.
  • Valid operands are of int types. Result must be a double type.
  • You may want to use a StringTokenizer object to assist in parsing the expression.
  • Handle exceptions. Catch these types of exception: (1) incorrect format (incorrect operand or operator), (2) too may operands, and (3) too many operators. You should check the postfix for correctness using try/catch statement(s). An invalid input might be " 2 3 4", " 2 A /", or "2 3 + +". You should report the error to the user and allow the user to enter another input. The program should not stop due to an error in the input.

Sample run including exception handling:

Enter a valid postfix equation ("exit to stop"): 5 3 + 2 /
Result is: 4.0

Enter a valid postfix equation ("exit to stop"): 12 8 - 6 2 + *
Result is: 32.0

Enter a valid postfix equation ("exit to stop"): 12 8 - X +
Check the input format please.

Enter a valid postfix equation ("exit to stop"): 12 8 - 6 2 +
Check the number of OPERANDS - looks like there's too many!

Enter a valid postfix equation ("exit to stop"): 12 8 - 6 2 + */
Check the number of OPERATORS - looks like there's too many!

Enter a valid postfix equation ("exit to stop"): 20 6 %
Check the input format please.

Enter a valid postfix equation ("exit to stop"): Exit
Enjoy your day!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.