Question: Write a small Java program that implements a very simple arithmetic calculator for integer values (you should support the five basic operations: addition, subtraction, multiplication,

Write a small Java program that implements a very simple arithmetic calculator for integer values (you should support the five basic operations: addition, subtraction, multiplication, division, and modulus). Your program should prompt the user to enter (on a single line, separated by whitespace) the two operands and the operation to be performed. It then prints the result on a new line. For example:

Please enter a calculation to perform: 1 + 2 3

Your program should include an exception-handling mechanism (a try-catch block) that deals with illegal operations (basically, division by 0 or an unrecognized arithmetic operator; you may assume that the user will always enter integer values for the operands). In the event that your program raises an exception, your program should display a message that informs the user of the type of error before it exits (without printing any type of answer):

Please enter a calculation to perform: 1 / 0 Division by zero is illegal

or

Please enter a calculation to perform: 4 $ 22 $ is an invalid operator

Recall that you can declare an Exception with a String that describes the details of a particular abnormal situation, e.g., new Exception(Too few input values!).

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!