Program You are to write a program name calc.java that evaluates an infix expres
ID: 3683783 • Letter: P
Question
Program
You are to write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: Integer constants (a series of decimal digits). x (representing a value to be supplied later). Binary operators (+. *,/and %). Parentheses Spaces between tokens are allowed but not required. The program will convert the expression to postfix (RPN) form and display the converted expression. The program will repeatedly prompt the user for the value of x, each time. When the user enters the letter q instead of a number, the program terminates. The following example illustrates the behavior of the program (user input is in bold and red): If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors: The output of your program must match the format illustrated in this example.Explanation / Answer
import java.util;
class infix
{
public static void main(String args[])
{
String e;
double ans;
int x;
System.out.println("Enter infix expression");
e=scan.nextLine();
while(x!='q')
{
System.out.println("Enter value of x");
x=scan.nextInt();
if(x=='q')
break;
ans=(Number) e.evaluate(x);
System.out.println("Answer of expression: "+ans);
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.