Skip to content

Commit a8eca5e

Browse files
author
Sean Thames
committed
Finished exercise 2
1 parent e66e1b7 commit a8eca5e

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

m10/ex2/JPasswordA.java

+47
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,50 @@
1717
* to a list of five valid passwords: Rosebud, Redrum, Jason, Surrender,
1818
* or Dorothy. Save the file as JPasswordC.java
1919
*/
20+
21+
import javax.swing.*;
22+
import java.awt.*;
23+
import java.awt.event.*;
24+
25+
public class JPasswordA extends JApplet implements ActionListener
26+
{
27+
private final String PASSWORD = "Rosebud";
28+
29+
private Container con = getContentPane();
30+
private JLabel passwordLabel = new JLabel("Password:");
31+
private JTextField passwordField = new JTextField(16);
32+
33+
private JLabel accessGranted = new JLabel("<html><font color=\"green\">Access Granted</font></html>");
34+
private JLabel accessDenied = new JLabel("<html><font color=\"red\">Access Denied</font></html>");
35+
36+
public void init()
37+
{
38+
con.setLayout(new FlowLayout());
39+
40+
con.add(passwordLabel);
41+
con.add(passwordField);
42+
43+
con.add(accessGranted);
44+
accessGranted.setVisible(false);
45+
con.add(accessDenied);
46+
accessDenied.setVisible(false);
47+
48+
passwordField.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent ae)
52+
{
53+
String input = passwordField.getText();
54+
55+
if(input.equals(PASSWORD))
56+
{
57+
accessGranted.setVisible(true);
58+
accessDenied.setVisible(false);
59+
}
60+
else
61+
{
62+
accessDenied.setVisible(true);
63+
accessGranted.setVisible(false);
64+
}
65+
}
66+
}

m10/ex2/JPasswordB.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Programmer: Sean Thames
3+
* Date: 2013-07-25
4+
* Filename: JPasswordA.java
5+
* Assignment: Ch 17 Ex 2
6+
*
7+
* Description: A) Create a JApplet that asks a user to enter a
8+
* password into a JTextField and to then press Enter. Compare the
9+
* password to "Rosebud"; if it matches exactly, display "Access Granted".
10+
* If not, display "Access Denied". Save the file as JPasswordA.java.
11+
*
12+
* B) Modify the password applet in Exercise 2a to ignore differences in
13+
* case between the typed password and "Rosebud". Save the file as
14+
* JPasswordB.java.
15+
*
16+
* C) Modify the password applet in Exercise 2b to compare the password
17+
* to a list of five valid passwords: Rosebud, Redrum, Jason, Surrender,
18+
* or Dorothy. Save the file as JPasswordC.java
19+
*/
20+
21+
import javax.swing.*;
22+
import java.awt.*;
23+
import java.awt.event.*;
24+
25+
public class JPasswordB extends JApplet implements ActionListener
26+
{
27+
private final String PASSWORD = "Rosebud";
28+
29+
private Container con = getContentPane();
30+
private JLabel passwordLabel = new JLabel("Password:");
31+
private JTextField passwordField = new JTextField(16);
32+
33+
private JLabel accessGranted = new JLabel("<html><font color=\"green\">Access Granted</font></html>");
34+
private JLabel accessDenied = new JLabel("<html><font color=\"red\">Access Denied</font></html>");
35+
36+
public void init()
37+
{
38+
con.setLayout(new FlowLayout());
39+
40+
con.add(passwordLabel);
41+
con.add(passwordField);
42+
43+
con.add(accessGranted);
44+
accessGranted.setVisible(false);
45+
con.add(accessDenied);
46+
accessDenied.setVisible(false);
47+
48+
passwordField.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent ae)
52+
{
53+
String input = passwordField.getText();
54+
55+
if(input.equalsIgnoreCase(PASSWORD))
56+
{
57+
accessGranted.setVisible(true);
58+
accessDenied.setVisible(false);
59+
}
60+
else
61+
{
62+
accessDenied.setVisible(true);
63+
accessGranted.setVisible(false);
64+
}
65+
}
66+
}

m10/ex2/JPasswordC.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Programmer: Sean Thames
3+
* Date: 2013-07-25
4+
* Filename: JPasswordA.java
5+
* Assignment: Ch 17 Ex 2
6+
*
7+
* Description: A) Create a JApplet that asks a user to enter a
8+
* password into a JTextField and to then press Enter. Compare the
9+
* password to "Rosebud"; if it matches exactly, display "Access Granted".
10+
* If not, display "Access Denied". Save the file as JPasswordA.java.
11+
*
12+
* B) Modify the password applet in Exercise 2a to ignore differences in
13+
* case between the typed password and "Rosebud". Save the file as
14+
* JPasswordB.java.
15+
*
16+
* C) Modify the password applet in Exercise 2b to compare the password
17+
* to a list of five valid passwords: Rosebud, Redrum, Jason, Surrender,
18+
* or Dorothy. Save the file as JPasswordC.java
19+
*/
20+
21+
import javax.swing.*;
22+
import java.awt.*;
23+
import java.awt.event.*;
24+
25+
public class JPasswordC extends JApplet implements ActionListener
26+
{
27+
private final String[] PASSWORD = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"};
28+
29+
private Container con = getContentPane();
30+
private JLabel passwordLabel = new JLabel("Password:");
31+
private JTextField passwordField = new JTextField(16);
32+
33+
private JLabel accessGranted = new JLabel("<html><font color=\"green\">Access Granted</font></html>");
34+
private JLabel accessDenied = new JLabel("<html><font color=\"red\">Access Denied</font></html>");
35+
36+
public void init()
37+
{
38+
con.setLayout(new FlowLayout());
39+
40+
con.add(passwordLabel);
41+
con.add(passwordField);
42+
43+
con.add(accessGranted);
44+
accessGranted.setVisible(false);
45+
con.add(accessDenied);
46+
accessDenied.setVisible(false);
47+
48+
passwordField.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent ae)
52+
{
53+
String input = passwordField.getText();
54+
55+
for(String p : PASSWORD)
56+
{
57+
if(input.equalsIgnoreCase(p))
58+
{
59+
accessGranted.setVisible(true);
60+
accessDenied.setVisible(false);
61+
}
62+
else
63+
{
64+
accessDenied.setVisible(true);
65+
accessGranted.setVisible(false);
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)