Java Reference
In-Depth Information
then will display an appropriate message. The file includes a method
called setEchoCharacter(), which displays asterisks as the user types the
password and a setLayout() method to align the components.
4 Using an Applet to Search an Array (continued)
Using the Login.java file from the Data Disk and the techniques
learned in this chapter, write the code to complete the applet as
shown in Figure 5-51. Include code to create two arrays, one for the
IDs and one for the corresponding passwords. Assign some sample
IDs and passwords to elements of the arrays. Write a for loop that
loops through the array, searching for a valid ID and password. Use
an if statement in the for loop to look at each member of the array
and set a flag to true if the ID and password match an array member.
Part A: Creating the Host Document
FIGURE 5-51
1. Start TextPad. When the coding window opens, type the HTML
tag and then, on the next line, begin the APPLET tag.
2. Use a CODE statement to access the PasswordApplet.class. Use a WIDTH
statement of 300 and a HEIGHT statement of 300.
3. Close the APPLET and HTML tags.
4. Save the file on the Data Disk, using PasswordApplet.html as the file name.
Part B: Creating the Applet
1. Use TextPad to open the Login.java file from the Chapter05 folder on the
Data Disk. Enter your name and date in the block comment at the begin-
ning. Type PasswordApplet as the new class name. Edit the name of the
class in the class header as well.
2. Save the file on the Data Disk, using PasswordApplet.java as the file name.
Print a copy if you wish, to reference while completing this lab.
3. Insert three import statements after the block comment to import the
java.awt.*, java.applet.*, and java.awt.event.* packages.
4. Insert the following array declaration and assignment statements at the end
of the Declaring variables section. Fill in your own choice of passwords and
IDs. Separate each element of the array with a comma, and enclose each
password and ID in quotations.
String idArray[] = {“id”, …};
String passwordArray[] = {“ password”, …};
5. Below the //Sequential search comment line in the actionPerformed()
method, construct a for loop that uses i as its counter. The for loop should
begin at 0, continue while the counter is less than idArray.length, and
increment by 1 using the unary operator.
6. Inside the for loop, enter the following code, which uses the compareTo()
methods to look for a match in both the id and password.
Search WWH ::




Custom Search