-
Your calculator will need to maintain its state. While we won't have a display like a normal calculator, a
Calculator object will still need to remember its current value so that it knows what to do when we add to the current value, subtract from it, etc. Define a new field at the top of the class that will hold the calculator's value. Make sure you give the field a meaningful name so the rest of your code is easier to read.
-
Next, define two constructors: The first should initialize the state to be zero, and needs no parameters. The second should take a single integer argument, and set the calculator's initial value to the specified value.
-
Now add a method called
add that takes a single
integer argument. It should set the calculator's state to be the
sum of its old value and the argument to
add. It should also print a message describing the
inputs to the addition and the result, as shown below. (In this
output, lines starting with ">" have been typed into
the BlueJ codepad, and the corresponding output in the terminal window
is shown below each statement.)
> Calculator c1;
> c1 = new Calculator();
> c1.add(5);
0 + 5 = 5
> c1.add(10);
5 + 10 = 15
> Calculator c2;
> c2 = new Calculator(100);
> c2.add(61);
100 + 61 = 161
-
After verifying that your constructors and
add method work, implement the subtract and multiply methods. They should each take a single integer argument, like add. The subtract method should subtract its argument from the calculator's value, and multiply should multiply its argument by the current value. Each should print a single line of output describing the arithmetic operation, its inputs, and result:
> Calculator c;
> c = new Calculator(20);
> c.multiply(3);
20 * 3 = 60
> c.subtract(10);
60 - 10 = 50
> c.multiply(2);
50 * 2 = 100
-
Now it's time to implement
divide. It should work like the other three arithmetic methods, but with two differences: First, it should print a message if the denominator is zero, and clear the calculator's value. Second, it should print an additional message if the division will result in a loss of precision. Since our calculator only works with integers, some divisions will cause the result to be truncated. (Hint: Don't forget about the mod operator.)
> Calculator c;
> c = new Calculator(40);
> c.divide(2);
40 / 2 = 20
> c.divide(3);
Warning: division resulted in loss of precision.
20 / 3 = 6
> c.divide(2);
6 / 2 = 3
> c.divide(-1);
3 / -1 = -3
> c.divide(0);
Error: division by zero -- value cleared.
-3 / 0 = 0
-
Next, implement the memory operations. Start with
store, which takes no arguments, but causes the calculator's current value to be stored in a memory location. (Hint: You'll need to add a new field to your class.) Then implement recall, which takes no arguments, but causes the stored value to replace the calculator's current value. For example:
> Calculator c;
> c = new Calculator(20);
> c.add(3);
20 + 3 = 23
> c.store();
23 --> mem
> c.multiply(5);
23 * 5 = 115
> c.recall();
mem --> 23
> c.add(10);
23 + 10 = 33