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

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!