Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^251


Testing:Given the following data on the file gender.dat


the application Incomeswrites the following on the file results.dat:


With an EOF-controlled loop, the obvious test cases are a file with data (as that
shown above) and an empty file. We should test input values of both ‘F’ and ‘M’ for the
gender, and try some typical data (so that we can compare the results with our hand-
calculated values) and some atypical data (to see how the process behaves). An atypical
data set for testing a counting operation is an empty file, which should result in a count
of zero. Any other result for the count indicates an error. For a summing operation,
atypical data might include negative or zero values.
The Incomesapplication is not designed to handle empty files or negative income val-
ues; the assumption that the file was not empty and contained at least one male and
one female was written into the design. An empty file causes both femaleand maleto
have countequal to zero at the end of the loop. Although this result is correct, the state-
ments that compute the average income cause the application to produce invalid
results (infinity) because they divide by zero. A negative income would be treated like
any other value, even though it is probably a mistake.
To correct these problems, we should insert ifstatements to test for the error condi-
tions at appropriate points in the application. When an error is detected, the
application should either display an error message or write an error report to a file
instead of carrying out the usual computation.

Free download pdf