Java Reference
In-Depth Information
maxHistory ACCESSOR AND MUTATOR METHODS Figure 9-20
displays the pseudocode that outlines the logic for the setMaxHistory() method.
Figure 9-21 displays the code for the accessor method and mutator method for
the maxHistory instance variable of the Password class.
only done if
size of password
history list > max
history limit
FIGURE 9-20
90
public int getMaxHistory ()
91
{
92
return maxHistory;
93
}
94
95
public void setMaxHistory ( int newMaxHistory )
96
{
97
int overage = 0;
98
if ( newMaxHistory >= 1 && newMaxHistory <= 10 )
99
{
100
maxHistory = newMaxHistory;
101
overage = getHistorySize () - maxHistory;
102
if ( overage > 0 )
// if size > max allowed
103
{
104
do {
105
pswdHistory.remove ( 0 ) ;
// then remove overage number
106
overage--;
// of oldest pswds from list
107
} while ( overage > 0 ) ;
108
109
pswdHistory.trimToSize () ;
// resize capacity to max allowed
110
}
111
}
112
}
FIGURE 9-21
This pair of accessor and mutator methods is similar to the pairs coded in the
previous steps. The accessor method, getMaxHistory(), in lines 90 through 93 sim-
ply returns the value of the instance variable, maxHistory. The mutator method,
setMaxHistory(), is somewhat more complex. First, the setMaxHistory() method
verifies that the new value is within the acceptable range of values for passwords
stored in the password history list — that is, between 1 and 10, inclusively, as
specified in the requirements document (line 98). If the new value is not valid, it is
Search WWH ::




Custom Search