Create a project in Eclipse called P3. Create a class in the project called P3,
ID: 3743647 • Letter: C
Question
Create a project in Eclipse called P3.
Create a class in the project called P3, without a main method!
Copy the starting code shown above into the class.
P3.java should not have a main method!
Download the graphical user interface from here.
Declare double variables for both operands and result.
Declare String variables for the operator.
Create a Scanner to parse the expression, as discussed in class.
Use the Scanner to check whether the first operand is a double.
If so, read it into the first operand variable.
If not, return "Invalid Operand!", thereby exiting the method.
Use the Scanner to check whether there is an operator.
If so, read it into the operator variable.
If not, return "Invalid Operator!", thereby exiting the method.
Make sure the operator variable is a "+", "-", "*", "/", "%", or "^".
HINT: A switch statement with a default case might be useful.
If not, return "Invalid Operator!", thereby exiting the method.
Use the Scanner to check whether the second operand is a double.
If so, read it into the second operand variable.
If not, return "Invalid Operand!", thereby exiting the method.
Using a switch statement based on the operator, evaluate the expression.
When complete, the result variable should contain the result of the expression.
The operator generally corresponds to the Java operator, i.e. "*" for multiplication.
One exception is the "^" operator, which requires special handling.
The "^" operator is exponent, which raises the first operand to the power specified by the second operand.
By far the easiest way to implement the exponent operator is using Math.pow().
Several examples of input expressions and the return strings are shown below.
Use the Double wrapper class to convert the numerical result to a string, for example:
CODE: String returnString = Double.toString(result);
Return the converted string, thereby exiting the method.
You do not have to close the Scanner, but you can if you want to.
Sample Output
Expression (input string) Result (return value) "11.22 + 3.456 = " "14.676 " "5.555 - 32.14 = " "-26.585 " "25634.8 * .32 = " "8203.136 " "2.1 / 55.3377 = " "0.037948812473232535 " "12345 % 23 = " "17.0 " "5 ^ 4 = " "625.0 " "Whatever + 2 = " "Invalid Operand! " "4.0 - 1.2.3 = " "Invalid Operand! " "1.234 $ 0.5 = " "Invalid Operator! "Explanation / Answer
import java.util.Scanner; public class CalculateExpression { public static String calculate() { Scanner in = new Scanner(System.in); double num1, num2, result; String operator; try { num1 = Double.parseDouble(in.next()); } catch (Exception e) { return "Invalid Operand! "; } operator = in.next(); try { num2 = Double.parseDouble(in.next()); } catch (Exception e) { return "Invalid Operand! "; } switch (operator) { case "+": return String.valueOf(num1 + num2); case "-": return String.valueOf(num1 - num2); case "*": return String.valueOf(num1 * num2); case "/": return String.valueOf(num1 / num2); case "%": return String.valueOf(num1 % num2); case "^": return String.valueOf(Math.pow(num1, num2)); default: return "Invalid Operator! "; } } public static void main(String[] args) { System.out.println(calculate()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.