Self-Test 6
Wed, Dec. 1 2010
Exercise:
Today we will create a class called Calculator
that can add, subtract, multiply, and divide, as well as keep track of
the accumulated results of calculations.
Before you start:
Find a calculator on your system and run it. Do the following
calculations:
5 + 4 =
|
9 |
6 + 2 =
|
8 (previous result (9) is
lost)
|
10 + 7 =
|
17 (previous result (8) is lost)
|
+ 3 =
|
20 (3 added to previous result)
|
* 4 =
|
80 (previous result multiplied
by 4)
|
/ 8 =
|
10 (previous result divided by 8)
|
3 + 5 =
|
8 (previous result lost)
|
Your Calculator class should include the following methods:
- a method to get the result
- a method to clear the result
- 2 methods for each operation (add, subtract, mult, div):
- One that takes two double parameters and resets result
- One that takes one double parameter and uses result as one of
the operands
Write a demo program for light testing.
Write junit tests for more thorough testing.
Note that java can now handle division by 0 - you get Infinity insead
of an error...