Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

BODMAS (Brackets, Order, Division, Multiplication, Addition, Subtraction) or BIDMAS (Brackets, Indices, Division, Multiplication, Addition, Subtraction) refer to an order in which mathematical operations should be performed. To implement this order in your Java calculator code, you can follow these steps:

  1. Parse the user input: When the user enters a mathematical expression, you need to parse it to identify the individual parts of the expression, such as the numbers and the operations.

  2. Check for brackets: If the expression contains brackets, you need to evaluate the expression inside the brackets first. You can use recursion to evaluate the inner expressions first, and then use the result to evaluate the outer expression.

  3. Evaluate indices: If the expression contains indices (powers or roots), you need to evaluate them next. You can use the Math.pow() method to calculate the power of a number, and the Math.sqrt() method to calculate the square root.

  4. Perform division and multiplication: Next, you need to perform any divisions or multiplications from left to right.

  5. Perform addition and subtraction: Finally, you need to perform any additions or subtractions from left to right.

  6. Return the result: Once you have evaluated all the parts of the expression in the correct order, you can return the final result to the user.

Here is a sample code that implements BIDMAS in a Java calculator:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a mathematical expression: ");
        String expression = scanner.nextLine();

        double result = evaluateExpression(expression);
        System.out.println("Result is: " + result);
    }

    private static double evaluateExpression(String expression) {
        double result = 0.0;

        // Evaluate brackets first
        while (expression.contains("(")) {
            int leftBraceIndex = expression.lastIndexOf("(");
            int rightBraceIndex = expression.indexOf(")", leftBraceIndex);
            String innerExpr = expression.substring(leftBraceIndex + 1, rightBraceIndex);

            double innerResult = evaluateExpression(innerExpr);
            expression = expression.substring(0, leftBraceIndex) + innerResult + expression.substring(rightBraceIndex + 1);
        }

        // Evaluate indices next
        while (expression.contains("^")) {
            int index = expression.indexOf("^");
            double base = Double.parseDouble(expression.substring(index - 1, index));
            double exponent = Double.parseDouble(expression.substring(index + 1, index + 2));
            double innerResult = Math.pow(base, exponent);

            expression = expression.substring(0, index - 1) + innerResult + expression.substring(index + 2);
        }

        // Evaluate division and multiplication
        int index = 0;
        while (index < expression.length()) {
            char operator = expression.charAt(index);

            if (operator == '/') {
                double leftOperand = Double.parseDouble(expression.substring(0, index));
                double rightOperand = Double.parseDouble(expression.substring(index + 1));
                result = leftOperand / rightOperand;
                expression = result + expression.substring(index + 1);
            } else if (operator == '*') {
                double leftOperand = Double.parseDouble(expression.substring(0, index));
                double rightOperand = Double.parseDouble(expression.substring(index + 1));
                result = leftOperand * rightOperand;
                expression = result + expression.substring(index + 1);
            } else {
                index++;
            }
        }

        // Evaluate addition and subtraction
        index = 0;
        while (index < expression.length()) {
            char operator = expression.charAt(index);

            if (operator == '+') {
                double leftOperand = Double.parseDouble(expression.substring(0, index));
                double rightOperand = Double.parseDouble(expression.substring(index + 1));
                result = leftOperand + rightOperand;
                expression = result + expression.substring(index + 1);
            } else if (operator == '-') {
                double leftOperand = Double.parseDouble(expression.substring(0, index));
                double rightOperand = Double.parseDouble(expression.substring(index + 1));
                result = leftOperand - rightOperand;
                expression = result + expression.substring(index + 1);
            } else {
                index++;
            }
        }

        return Double.parseDouble(expression);
    }
}