Java Reference
In-Depth Information
ignored. If the new value is valid, the setMaxHistory() method assigns the new
value to the instance variable, maxHistory (line 100). Line 101 then subtracts
maxHistory from the current history size (ascertained by a method call to the
accessor method). The result of the subtraction is assigned to a variable named
overage. If the new value assigned to overage is greater than zero (line 102), the
oldest passwords in the history must be removed until the history is not larger
than the new value represented by maxHistory. The ArrayList class does have a
method to remove a range of values; however, that method is protected and not
available for use by methods in the Password class.
The only accessible method, the remove() method , removes elements from
the ArrayList one at a time. Lines 104 through 107 thus include a do…while
loop that removes the older passwords from the list until the overage number is
equal to zero. Line 109 uses the trimToSize() method to reset the capacity of the
ArrayList, pswdHistory, to the new maximum size, which is also the number of
current elements. The remove() and trimToSize() methods are discussed in more
detail later in the chapter, along with additional ArrayList methods.
The following steps enter the code for the accessor method and mutator
method for the maxHistory instance variable of the Password class.
To Code Accessor and Mutator Methods for the maxHistory
Instance Variable
1. Enter lines 90 through 93 as shown in Figure 9-21 on the previous page.
TextPad displays the accessor method for the maxHistory variable in the
coding window (Figure 9-22).
accessor
method
FIGURE 9-22
2. Enter lines 95 through 112 as shown in Figure 9-21.
TextPad displays the mutator method for the maxHistory variable in the coding
window (Figure 9-23). If the new history size is greater than or equal to the cur-
rent size, no old password elements are removed from the password history list.
Search WWH ::




Custom Search