Java Reference
In-Depth Information
anything other than a legal month number (integers from 1 to 12 ), your program
will throw and catch a MonthException and ask the user to reenter the month.
Similarly, if the user enters anything other than a valid day number (integers from 1 to
either 28 , 29 , 30 , or 31 , depending on the month and year), then your program will
throw and catch a DayException and ask the user to reenter the day. If the user enters
a year that is not in the range 1000 to 3000 (inclusive), then your program will throw
and catch a YearException and ask the user to reenter the year. (There is nothing
very special about the numbers 1000 and 3000 other than giving a good range of likely
dates.) See Self-Test Exercise 19 in Chapter 4 for details on leap years.
6. Write a 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 for “result” (either in upper- or
lowercase). 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. Defining the class
UnknownOperatorException is part of this project.
Calculator is on.
result = 0.0
+5
result + 5.0 = 5.0
new result = 5.0
* 2.2
result * 2.2 = 11.0
updated result = 11.0
% 10
% is an unknown operation.
Reenter, your last line:
* 0.1
result * 0.1 = 1.1
updated result = 1.1
r
Final result = 1.1
Again? (y/n)
yes
result = 0.0
+10
result + 10.0 = 10.0
new result = 10.0
/2
result / 2.0 = 5.0
Search WWH ::




Custom Search