Class PostfixEvaluation

java.lang.Object
com.project.calculator.PostfixEvaluation

public class PostfixEvaluation extends Object
The PostfixEvaluation class evaluates a mathematical expression in postfix notation using a stack-based approach.

This class performs the following tasks:

  • Processes postfix expressions containing operands and operators.
  • Evaluates the expression using a stack.
  • Updates the result in the MainUI instance.
  • Constructor Details

    • PostfixEvaluation

      PostfixEvaluation(List<String> expression, MainUI mainUiInstance)
      Constructs a PostfixEvaluation object and evaluates the given postfix expression.

      The result is stored in the MainUI instance for display.

      Parameters:
      expression - The postfix expression to evaluate (as a List<String>).
      mainUiInstance - The MainUI instance to store the result.
  • Method Details

    • evaluatePostfix

      private double evaluatePostfix(List<String> expression)
      Evaluates the given postfix expression.

      The method uses a stack to:

      • Push operands onto the stack.
      • Pop two operands when an operator is encountered.
      • Perform the operation and push the result back onto the stack.
      Parameters:
      expression - The postfix expression as a List<String>.
      Returns:
      The result of the evaluated expression as a double.
    • isOperator

      public static boolean isOperator(String token)
      Checks if the given token is a mathematical operator.

      Supported operators: +, -, *, /, %.

      Parameters:
      token - The token to check.
      Returns:
      true if the token is an operator, false otherwise.
    • performOperation

      public static double performOperation(double num1, double num2, String operator)
      Performs a mathematical operation on two operands based on the given operator.

      The supported operations include:

      • + → Addition
      • - → Subtraction
      • * → Multiplication
      • / → Division
      • % → Modulus
      Parameters:
      num1 - The first operand.
      num2 - The second operand.
      operator - The mathematical operator as a String.
      Returns:
      The result of the operation as a double.