Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a JApplet that asks a user to enter a password into a JTextField and to t

ID: 3652983 • Letter: C

Question

Create a JApplet that asks a user to enter a password into a JTextField and to then press Enter. Compare the password to "Rosebud" If it matches exactly, display "Access Granted" if not, display "Access Denied". Save the file as JPasswordA.java Part B: Modify the password applet in Exercise 2a to ignore differences in case between the typed password and Rosebud. Save the file as JPasswordB.java Part C: Modify the password apple in Exercise 2b to compare the password to a list of five valid passswords. Rosebud, Redrum, Jason, Surrender, or Dorothy. Save the file as JPasswordC.java

Explanation / Answer

Code: /* Create a Swing applet that asks a user to enter a password XXXXX a JTextField and to then press [Enter]. Compare the password XXXXX "Rosebud"; if it matches, display "Access Granted"; if not, display "Access Denied". @author Karyn Choi @version 1.01 */ import javax.swing.*; import java.awt.event.*; import java.awt.*; public class JPasswordA extends JApplet implements ActionListener { Container PW = getContentPane(); JLabel password = new JLabel("Please enter the password"); JTextField input = new JTextField(7); JButton enter = new JButton("Enter"); public void start() { PW.add(password); PW.add(input); PW.add(enter); PW.setLayout(new FlowLayout()); enter.addActionListener(this); } public void actionPerformed(ActionEvent e) { String pass1 = input.getText(); String pass2 = "Rosebud"; if(pass1.equalsIgnoreCase(pass2)) { JOptionPane.showMessageDialog(null, "Access Granted :/)"); } else { JOptionPane.showMessageDialog(null, "Access Denied :/("); } } } You should compile it using javac JPasswordA.java and run using Appletviewer JPassword.java