Your instructor will assign one or more of the following problems. Submit all appropriate files for grading, including code files, screen captures, supplemental files (e.g., image files), and text files.
For this assignment, extend the calculator you built in the lab to do the following:
For example, if the user uses the calculator to compute “1” + “1”, and then computes “memory” + “3”, the answer should be 5 (i.e., the remembered value 2 plus 3). There are different ways to implement these features. Here is one way:
Calculator
- Modify the Calculator
class to: store the
remembered value in an instance variable; initialize the instance variable to 0.0; provide an
accessor method for the memory value; and include code to update the memory value whenever a new
computation is made.CalculatorController
- Modify the actionPerformed()
method in
the CalculatorController
class to detect the use of the string “memory”
in either of the operand fields and replace that string with value accessed from the calculator
object. Note that because the calculate()
method receives double values, the
controller cannot pass it the string value “memory”. Thus, your controller must
replace the string “memory” with the appropriate remembered value from the calculator,
a double, before calling calculate()
.CalculatorTest
- Include an appropriate unit test case for the
calculator’s memory feature. Note that because your CalculatorController
is
handling the “memory” string, you cannot build a unit test exercising that part of
your implementation; you only need to unit test the memory features in the Calculator
class itself (i.e., that initial memory value is 0.0 and that the memory value changes on every
new calculation).Extra-credit: Implement the calculator so that the memory value is
undefined at first. If the user tries to use the memory feature before performing a calculation,
the Calculator
class should throw an appropriate exception and the CalculatorController
should handle that exception. Be sure to unit test this feature.
Note that while the lab version of the calculator could have been implemented as a static library, this memory extension requires the use of instance variables and methods.