Java Reference
In-Depth Information
When we run the test case again, we see that method anglicize was called
with n = 1210 , and the result was wrong. Here is the output:
Start anglicize, n = 1210
End anglicize, ans = nine hundred nine
A look at the precondition of anglicize tells us that anglicize was called
incorrectly because 1210 is not in the range for which it was written. Therefore,
we will have to look for the error at the place where this call was made.
We do not pursue this particular error further. Instead, we look closer at
method anglicize itself.
Tracking down another bug
It takes time to insert and remove print statements and to run tests in order
to detect bugs. Therefore, we try to follow this maxim:
Debugging maxim 2. Glean as much information as possible
from each exercise of test cases.
In the case discussed above, it does not appear that calling anglicize with n
= 1210 should produce the result that it did. After all, the then-part of the first if-
statement will set k to 10 , so where did the "nine" come from in the answer?
If we do not see the error, we:
Debugging maxim 3. Insert print statements at judiciously cho-
sen places, in order to check values of variables.
In this case, we place the same print statement after each if-statement:
System.out.println("s = " + s + " , k = " + k);
And we execute the program again (with the same input, of course). Here is the
output from that execution:
Start anglicize, n = 1210
s= nine hundred, k= 10
s= nine hundred, k= 10
s= nine hundred, k= 10
s= nine hundred nine, k= 0
End anglicize, ans= nine hundred nine
The output of the first four print statements is reasonable. But the output of
the fifth print statement is not. Why did the call to digitName produce " nine"
when its argument was 10 ? To find this out, we have to look at the specifications
of the methods being called (see Fig. 4.6).
A look at the specification of digitName reveals that that method requires
its argument to be less than 10 , so it is our mistake to call digitName with 10 as
the argument. But then where is the integer 10 handled?
We back up to the previous if-statement and take a look at the specification
 
Search WWH ::




Custom Search