Explore BrainMass

Simple Calculator in JAVA

Write a Java program that can serve as a simple calculator. This calculator keeps track of a single number (of type double) that is called result and that starts out as 0.0. Each cycle allows the user to repeatedly add, subtract, multiply, or divide by a second number. The result of one of these operations becomes the new value of result. The calculation ends when the user enters the letter "R" or "r" for 'result'. The user is allowed to do another calculation from the beginning as often as desired.
The input format is shown in the following sample dialogue. If the user enters any operator symbol other than "+,-,*, or /", then an UnknownOperatorException is thrown and the user is asked to reenter that line of input. Define the class UnknownOperatorException appropriately.

Calculator is on
result = 0.0
result + 5.0 = 5.0
new result = 5.0
result * 2.2 = 11.0
updated result = 11.0
% is an unknown operation.
Reenter, your last line:
result * 0.1 = 1.1
updated result = 1.1
Final result = 1.1
Do you want calculate again? (y/n)
result = 0.0
result + 10.0 = 10.0
new result = 10.0
result / 2.0 = 5.0
updated result = 5.0
Final result = 5.0
Do you want calculate again? (y/n)
End of Program

Solution Summary

A simple calculator in JAVA is provided.