CASE STUDY^247
rations for the fields include initialization, then the constructor doesn’t have to do any-
thing. In such a case, Java lets us omit the constructor, and it provides a default
constructor for the class that does nothing. We can just strike the constructor off our
list. That’s about as easy as it can get!
Updating is very simple. This operation just increments the count and adds its argu-
ment to the total salary. Note that it is a void method.
The knowCount responsibility just returns the count field.
For the knowAverage responsibility, we need to return the result of dividing the total
salary by the count, remembering to cast the value of the count to type double.
That completes the design for theGenderclass. Now we’re ready to implement it in Java.
//
// This class keeps the statistics for an individual
// gender in processing a set of incomes.
//
classGender
{
intcount = 0 ;
doubletotalSalary = 0.0;
// Transformer to update state
public voidupdate(doublesalary)
{
count++;
totalSalary = totalSalary + salary;
}
// Returns count of salary values added with update
public intknowCount()
{
returncount;
}
knowAverage () returns double
return total salary / (double) count
knowCount () returns int
return count
void update (double salary)
increment count
add salary to total salary