Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

summarise Article 1 of the Constitution of the Republic of Namibia.

Answered: 1 week ago