CASE STUDY
246
AVERAGE INCOME BY GENDER
Problem:You’ve been hired by a law firm that is working on a sex discrimination case.
Your firm has obtained a file of incomes,gender.dat, which contains the salaries for
every employee in the company being sued. Each salary amount is preceded by “F” for
female or “M” for male. As a first pass in the analysis of these data, you’ve been asked
to compute the average income for females and the average income for males. The
number of males and the number of females should be output as well. The output
should be saved on a file for later review.
Discussion:Most of the nouns in this problem statement (which are usually good
indicators of potential objects) relate to the reason for the problem, not the problem it-
self. There are two file objects: one for input and one for output. The result of the appli-
cation is a pair of averages and a pair of counts, one of each for males and for females.
These averages are doublevalues, not classes. However, male and female are two
instances of a single concept: gender. Sometimes a problem statement may list specific
instances of objects, and it’s our job to identify the class that describes those instances
in general. We will begin by creating a Genderclass.
What state and responsibilities do we need to support for a Genderobject? Well, we
need to provide a count of the number of this particular gender and the average salary.
That task can be handled via value-returning methods. The average is the total salary
divided by the count, so we need to keep a running total along with the count. We also
need a constructor to create an object and initialize those values. The only responsibil-
ity left is to update the values each time this particular gender is input. Because we’re
changing the initial state of the object, we know that the object is mutable and this re-
sponsibility is a transformer. Let’s list the state and responsibilities of a Genderobject:
State
count (initially 0)
total salary (initially 0.0)
Responsibilities
constructor
update (double salary)
knowCount returns int
knowAverage returns double
Next, we go through the responsibilities, one by one, designing their algorithms. The
constructor needs no arguments, as it merely initializes the fields. In fact, if the decla-