Java Reference
In-Depth Information
explicitly checks to see that previousOperator contains either a + or a - before applying
it. So another value will not result in an incorrect operation being applied. By the end of this
method, the value of leftOperand will have been changed, so we would note its new value
in the state table.
6
Returning to the plus method, the remaining two fields have their values set, so the next
row of the state table will contain the following values:
plus 0 3 '+'
The walkthrough of the engine can be continued in a similar fashion, documenting the state
changes, gaining insights into its behavior, and raising questions along the way. The following
exercises should help you complete the walkthrough.
Exercise 7.25 Complete the state table based on the following subsequent calls found in
the testPlus method:
numberPressed(4);
equals();
getDisplayValue();
Exercise 7.26 When walking through the equals method, did you feel the same reassur-
ances that we felt in applyPreviousOperator about the default value of previousOp-
erator ?
Exercise 7.27 Walk through a call to clear immediately following the call to getDis-
playValue at the end of your state table, and record the new state. Is the engine in the same
state as it was at the previous call to clear ? If not, what impact do you think this could have
on any subsequent calculations?
Exercise 7.28 In the light of your walkthrough, what changes do you think should be made
to the CalcEngine class? Make those changes to a paper version of the class, and then try
the walkthrough all over again. You should not need to walk through the CalcEngineTester
class, just repeat the actions of its testAll method.
Exercise 7.29 Try a walkthrough of the following sequence of calls on your corrected ver-
sion of the engine:
clear();
numberPressed(9);
plus();
numberPressed(1);
minus();
numberPressed(4);
equals();
What should the result be? Does the engine appear to behave correctly and leave the correct
answer in displayValue ?
 
Search WWH ::




Custom Search