Question: Calculator.java From the code below please complete the infix, postfix and infixToPostfix methods. Postfix- All tokens will be separated by space. Accomodate negative values. Detect

Calculator.java

From the code below please complete the infix, postfix and infixToPostfix methods.

Postfix- All tokens will be separated by space. Accomodate negative values. Detect if there are not enough operands for an operator, or when there are extra operators, or missing operators. an exception should be throwin in these situations (IllegalArgumentException). These ^, *, /, +, - operators should be calculated.

Infix- This is the order of precedence for the operators. it should support multiple levels of parentheses. Values should be double. throw an IllegalArgumentException just like postfix.

  • ( )
  • ^ (power, i.e. 5 ^ 2)
  • * / (multiplication and division)
  • + - (addition and subtraction)
  • $ (reference operator that has lowest precedence)

infixToPostfix- Use a StringBuilder object to build your Postfix string to output from the method.

-tester class should tests all methods and error conditions as shown above.

Calculator.java

import java.util.Scanner; import java.lang.IllegalArgumentException;

public class Calculator { private static Stack valStack; private static Stack opStack;

public static double evaluatePostfix(String s) throws IllegalArgumentException { return 0; } //evaluate the given Infix String s public static double evaluateInfix(String s) { return 0; } private static void repeatOps(char refOp) { } private static void doOp() { }

public static int prec(char refOp) { int p = -1; switch(refOp) { } return p; } public static String infixToPostfix(String exp) { return exp; }

}

Tester.java

public class Tester { public static void main(String[] args) { System.out.println("2 + 11 * 22 * 33 / 44 = " + Evaluator.evaluateInfix("2 + 11 * 22 * 33 / 44")); System.out.println("-4 5 3 + - 2 * 1 + = " + Evaluator.evaluatePostfix("-4 5 3 + - 2 * 1 +")); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!