Question: Can someone please help me with how to write this in Java? You will write a program to evaluate a math expression given by a

Can someone please help me with how to write this in Java?

You will write a program to evaluate a math expression given by a user.

Read the input from the scanner

Expressions should match the forms below. Note that you will need spaces between the numbers and operators for your scanner methods to work best.

- number1 operator number2

- operator number1

Declare number1 and number2 as type integer. Test using integer values.

You must support the following binary operations:

- ^ : exponent ( 3 ^ 2 means 3 raised to the 2nd power

- * : multiplication ( 3 * 2 means 3 multiplied by 2)

- + : addition (3 + 2 means 3 plus 2)

- / : division ( 3 / 2 means 3 divided by 2, * if you want a result with decimals you will need to cast one of the numbers to double)

You must support the following unary operations:

- # : square root (# 5 means the square root of 5)

- ~ : absolute (~ -5 means the absolute value of -5)

Your program should display the result of evaluating the expression in an appropriate form. For instance, a square root should be a double or float.

Hints:

You can use nested if statements for all your logic, but you may want to consider a switch statement for your binary operations.

When you read from the scanner, read the next string then decide what to do, is it a #? Is it a ~? and so on. These checks may mean moving on to read the next piece of data as an integer and display the result, or moving on to do a binary operation.

Name your class program MathExpressions.

Sample output:

Example 1: Enter a math expression: ~ -75 The absolute value of -75 is: 75

Example 2: Enter a math expression: # 50 The square root of 50 is 7.07

Example 3: Enter a math expression: 3 * 4 The product of 3 and 4 is 12

Example 4: Enter a math expression: 5 ^ 2 5 raised to the 2 power is 25

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!