Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^459


MONTHLY RAINFALL AVERAGES


Problem:Meteorologists have recorded monthly rainfall amounts at several sites
throughout a region of the country. You have been asked to write an application that
reads one year’s rainfall amounts from sites within the region from a file and that
prints out the average of the 12 values for each of the sites on a separate file. The first
line of a data set consists of the name of the site, and the 12 values for the site follow
on the next line with exactly one blank in between each value. The data file is named
rainData.dat.


Brainstorming:This problem sounds familiar. In Chapter 8, we wrote an application that
took rainfall amounts from observers and calculated the running average. In that prob-
lem we used a window for input and output; this problem asks for file input and
output. Calculating an average should be the same, but where the calculation takes
place is different. In the previous problem the calculation took place in the handler for
the button event. This problem does not include any buttons.
There are other nouns in the problem, but they describe the context like region,coun-
try, and sites, so they are not objects in the solution. What are the objects? The applica-
tion and the file objects.


Filtering:This looks like a very sparse set of classes for a problem that includes calculat-
ing a series of averages. This problem has a process that is repeated over different data,
but we do not have any objects other than numeric values and files.
What about error conditions? We can’t determine what they might be at this stage,
other than to say that the data might have been entered incorrectly on the file. Let’s
add an exception class to our list of classes.
Before we look at the responsibilities of each class, let’s name them. Let’s call the ap-
plication class RainFalland the exception class DataSetException.


application
input file
output file
Free download pdf