Here\'s the instructions to show exactly what I\'m being asked if that helps any
ID: 3745380 • Letter: H
Question
Here's the instructions to show exactly what I'm being asked if that helps any:
All of the Java code you write should be contained in the evaluate method. For this assignment, you must follow the directions below exactly:
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.
1// P3 Assignmentl 6 7 import java.util.Scanner; 9 public class P3 { 10 public static String evaluate(String expression) { 12 13 double operande, operand1, result; String operator; System.out.println(expression); // Declare variables for operands, operator, result, and return value String returnString""; 15 L6 17 18 19 Scanner sc-new Scanner(System.in); // Use Scanner to read operands and operator System.out.print( "First number: "); operande-Double.parseDouble(sc.next()); 1 23 25 26 27 /7 Compute a numerical result for the expression /7 Convert numerical result to string //Return result 29 30 31 32 return returnString;Explanation / Answer
import java.util.Scanner; public class P3 { public static String evaluate(String expression) { double operand0, operand1, result; String operator; System.out.println(expression); Scanner in = new Scanner(expression); try { operand0 = Double.parseDouble(in.next()); } catch (Exception e) { return "Invalid Operand! "; } operator = in.next(); try { operand1 = Double.parseDouble(in.next()); } catch (Exception e) { return "Invalid Operand! "; } switch (operator) { case "+": return String.valueOf(operand0 + operand1); case "-": return String.valueOf(operand0 - operand1); case "*": return String.valueOf(operand0 * operand1); case "/": return String.valueOf(operand0 / operand1); case "%": return String.valueOf(operand0 % operand1); case "^": return String.valueOf(Math.pow(operand0, operand1)); default: return "Invalid Operator! "; } } public static void main(String[] args) { System.out.println(evaluate("5 * 3")); } }