This is an introduction to Java programming question. Suppose you work at the IT
ID: 665201 • Letter: T
Question
This is an introduction to Java programming question.
Suppose you work at the IT department of a university. The department is launching a project for a new course registration system. As the team’s programmer, you are assigned to implement a function that supports users to change password.
Below the specific requirements for the password change function:
1. The system shall provide a GUI for users to change their passwords;
2. The system shall ask a user to type the password two times;
3. To protect information, the password typed on the GUI will be masked;
4. The new password must comply with the password policy:
10 - 32 characters in length
must start with a letter
at least one lower case letter
at least one upper case letter
at least one number
If the new password does not comply with policy, the program shall display corresponding errors in red on the UI.
Hint: For the password text box, you can use the JPasswordField class .
Below are several sample runs of the code:
Sample run 1: Type “Ab12345” as the password
Sample run 2: Type “abc1234567” as the password
Sample run 3: Type “@abcdefghijk” as the password
Sample run 4: Type “a12345678BC” as the password
Explanation / Answer
import java.awt.CardLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; public class SwapFields { private static final String TEXT_FIELD = "text field"; private static final String PASS_FIELD = "pass field"; public SwapFields() { // TODO Auto-generated constructor stub } private static void createAndShowGui() { final JTextField textField = new JTextField(10); final JPasswordField passwordField = new JPasswordField(10); final CardLayout cardLayout = new CardLayout(); final JPanel cardPanel = new JPanel(cardLayout); cardPanel.add(textField, TEXT_FIELD); cardPanel.add(passwordField, PASS_FIELD); JToggleButton toggleBtn = new JToggleButton("Entering Password"); toggleBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent iEvt) { if (iEvt.getStateChange() == ItemEvent.SELECTED) { cardLayout.show(cardPanel, PASS_FIELD); passwordField.requestFocusInWindow(); } else { cardLayout.show(cardPanel, TEXT_FIELD); textField.requestFocusInWindow(); } } }); JButton showNamePasswordBtn = new JButton("Show Results"); showNamePasswordBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { System.out.println("Name: " + textField.getText()); // never do this! System.out.println("Pass: " + new String(passwordField.getPassword())); } }); JPanel mainPanel = new JPanel(); mainPanel.add(cardPanel); mainPanel.add(toggleBtn); mainPanel.add(showNamePasswordBtn); JFrame frame = new JFrame("SwapFields"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.