Need some help with this java assignment Compilers render “normal” infix express
ID: 3797261 • Letter: N
Question
Need some help with this java assignment
Compilers render “normal” infix expressions into postfix expressions. Postfix notation allows expressions to be written without parenthesis or the strange rules of infix evaluation, which greatly speeds up the processing of an expression during runtime. For example, the infix expression 2 + 3 would be written 2 3 + in postfix the infix expression ( 5.0 – 3.5 ) / 1.2 would be written 5.0 3.5 – 1.2 / in postfix A postfix expression is evaluated using a stack. Scanning the postfix expression from left to right, place each operand encountered on the stack top. When an operator is encountered, pop the top two operands off the stack, apply the operator and place the result on the stack top. When the expression has been scanned, the sole remaining value on the stack is the result.
In your Java project, create and call a method that receives a postfix expression in a parameter of type String, evaluates it and returns the result of the expression as a double. In your method, assume that all values and symbols in the parameter will each be separated by a space. Assume that the only operators will be +, -, * and /.
Call the method with the following postfix expressions and output the results of each call. 23+ 5.03.5–1.2/ 5.03.51.2-/
Explanation / Answer
package postfixevaluator;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import java.util.Stack;
public class PostfixEvaluator {
public static boolean isOperator(String token){
return ("+".equals(token)) || ("-".equals(token)) || ("*".equals(token)) || ("/".equals(token));
}
public static String evaluate(double op1, double op2, String opString){
double value = 0;
if(null != opString)switch (opString) {
case "+":
value = op2 + op1;
break;
case "-":
value = op2 - op1;
break;
case "*":
value = op2 * op1;
break;
case "/":
value = op2 / op1;
break;
}
String valueString = Double.toString(value);
return valueString;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Stack operandStack = new Stack();
String expression = JOptionPane.showInputDialog("Enter the postfix expression");
StringTokenizer tokens = new StringTokenizer(expression);
while(tokens.hasMoreTokens()){
String nextToken = tokens.nextToken();
nextToken = nextToken.trim();
if(PostfixEvaluator.isOperator(nextToken)){
if(operandStack.empty()){
JOptionPane.showMessageDialog(null,
"The entered expression is not a valid postfix expression: ", expression,
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
else{
try{
Object firstOp = operandStack.pop();
String op1String = (String) firstOp;
double op1 = Double.parseDouble(op1String);
Object secondOp = operandStack.pop();
String op2String = (String) secondOp;
double op2 = Double.parseDouble(op2String);
String value = PostfixEvaluator.evaluate(op1, op2, nextToken);
operandStack.push(value);
}
catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"The entered expression is not a valid postfix expression: " + expression, "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
else{
operandStack.push(nextToken);
}
}
Object operand = operandStack.pop();
String opString = (String) operand;
double value= Double.parseDouble(opString);
JOptionPane.showMessageDialog(null, "The value of the entered postfix expression is " + value,
"Results", JOptionPane.PLAIN_MESSAGE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.