Java Reference
In-Depth Information
code for the pswd string. In the encrypt() method, the obtained hash code then
is appended to the string using the StringBuffer append() method in line 241,
and the resulting string is returned from the method. Finally, the returned string
is obtained from the StringBuffer toString() method in line 243, which returns a
new String object — the return data type required by the encrypt() method.
The following step enters code for the encrypt() method of the Password class.
To Code the encrypt() Private Helper Method
1. Enter lines 221 through 245 as shown in Figure 9-39 on page 581.
TextPad displays the helper method, encrypt(), for the Password class
(Figure 9-40). The method manipulates the String parameter by using a
StringBuffer and then returns a new String object to the calling method.
creates new
StringBuffer from
last and first half
of password
access is
private; limited
to this class
gets
character at
index
helper method
to encrypt
plaintext
password
appends hash
code to
encrypted
password
returns
String value
of
StringBuffer
sets character
at index
FIGURE 9-40
The code for the Password class now is complete. This class does not support
a user interface, nor is it an application or applet that can be run. It is simply a
definition of a class that is usable by other programs for creating Password
objects. To build the class file for use with other programs, the changes must be
saved and the source file must be compiled. The steps on the next page save and
compile the Password class source file.
 
Search WWH ::




Custom Search