Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 24 (8 points) Write a Java function called calculate that simulates a simple calculator. Given a mathematical expression stored in a String, your function
Question 24 (8 points) Write a Java function called calculate that simulates a simple calculator. Given a mathematical expression stored in a String, your function should calculate a result and return it as a double. For example, if your function is passed the String "3.7 +4.2", it should return the number 7.9. Your calculator only needs to handle two operators, +' and and only expressions containing two operands and a single operator. The three components will always be separated by a single space between each. Your function should check for one possible error in the input - if the operator is something other than '+ or" then your calculator does not understand it. In this case, print out an appropriate error message and return O (zero). Ignore any other possible errors in the input. You do not need to write a main function. For example Function call: calculate("8.01+ 2.0") calculate("-8 0.8") Returns 10.01 6i 0 (Error message: invalid operator calculate("8/ 2.0")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started