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
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
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