I need to create a Java application that checks to see if a password meets certa
ID: 3564523 • Letter: I
Question
I need to create a Java application that checks to see if a password meets certain criteria. I need to use the Character, String and/or StringBuilder classes and the application requires two classes. The first class will check a string and see if it meets the conditions required for a valid password. The second class will ask the user for a password and reply to the user indicating whether the password is acceptable.
1. Create a class named PasswordChecker. This class will have a single constructor that is private and takes no arguments. The constructor will not do anything. The method will have no code in it. Because this class will not have a main method. It is designed to be reused by many applications. For this reason it will not have user supplied input or user output. The input to any method(s) will be arguments and the output(s) will be return values.
2. In the PasswordChecker class, create a public static method named checkPassword that accepts a String argument and returns a boolean value. This method should be static and should check the String argument and verify the String contains the following:
I need to create a Java application that checks to see if a password meets certain criteria. I need to use the Character, String and/or StringBuilder classes and the application requires two classes. The first class will check a string and see if it meets the conditions required for a valid password. The second class will ask the user for a password and reply to the user indicating whether the password is acceptable. 1. Create a class named PasswordChecker. This class will have a single constructor that is private and takes no arguments. The constructor will not do anything. The method will have no code in it. Because this class will not have a main method. It is designed to be reused by many applications. For this reason it will not have user supplied input or user output. The input to any method(s) will be arguments and the output(s) will be return values. 2. In the PasswordChecker class, create a public static method named checkPassword that accepts a String argument and returns a boolean value. This method should be static and should check the String argument and verify the String contains the following: ? at least one uppercase letter ? at least one lower case letter ? at least one number ? at least one special character (not a number, space, or letter) ? have at least 8 characters. This means it must have 8 or more characters. The password cannot have any spaces in it. 3. The checkPassword method should return true if the String meets all the conditions specified above. The checkPassword method should return false if the Strings fails to meet any one of the conditions specified above. 4. I may implement other methods in the PasswordChecker class if I wish. These methods will also need to be static. It is likely these methods will need to be private 5. Create a second class named PasswordInput. This class will have the main method and will be used to test the code in the PasswordChecker class. 6. In the PasswordInput class, prompt the user to enter a password using an input dialog like the one below. Call the checkPassword method of the PasswordChecker class passing the input string. 7. Display a message dialog like the one below if the checkPassword method returns true indicating the password is valid. 8. Display a message dialog like the one below if the checkPassword method returns false demonstrating the password is not valid. 9. The application should stop after the password is analyzed and the result dialog is displayed. That is the application should stop after displaying Figure 2 or Figure 3 above 10. I cannot use any global or class-wide variables, that is variables declared outside a method. 11. I do not need to program a reply when the user clicks the Cancel button or clicks the X in the upper right corner of the dialog. 12. I need to addd a comment to my source codeExplanation / Answer
=============================
PasswordChecker.java
============================
public class PasswordChecker {
private PasswordChecker(){
}
public static boolean checkPassword(String password){
char[] charArr = password.toCharArray();
boolean upperCase = false;
boolean lowerCase = false;
boolean number = false;
boolean specialChar = false;
boolean containsSpace = false;
boolean isLongerThan8 = false;
for(int i=0; i<charArr.length; i++){
if(Character.isUpperCase(charArr[i]))
upperCase = true;
if(Character.isLowerCase(charArr[i]))
lowerCase = true;
if(Character.isDigit(charArr[i]))
number = true;
if(Character.isSpaceChar(charArr[i]))
containsSpace = true;
if(isSpecialChar(charArr[i]))
specialChar = true;
}
if(password.length()>=8)
isLongerThan8 = true;
if(upperCase && lowerCase && number && specialChar && isLongerThan8 && !containsSpace)
return true;
else
return false;
}
/*
* Special chracters check
*/
private static boolean isSpecialChar(char c){
String specialCharacters = "/*!@#$%^&*()"{}_[]|\?/<>,.";
for(char x : specialCharacters.toCharArray()){
if(x==c){
return true;
}
}
return false;
}
}
======================================
PasswordInput.java
======================================
import javax.swing.JOptionPane;
public class PasswordInput {
public static void main(String[] args) {
String password = JOptionPane.showInputDialog(null, "Please enter your password", "Password", JOptionPane.QUESTION_MESSAGE);
boolean valid = PasswordChecker.checkPassword(password);
if(valid)
JOptionPane.showMessageDialog(null, "Password is valid!", "Password Check Result", JOptionPane.INFORMATION_MESSAGE);
else{
String errorMessage = "Password is Inavlid! Password must contain a letter, a number, a special character and be 8 characters or longer with no spaces";
JOptionPane.showMessageDialog(null,errorMessage , "Password Check Result", JOptionPane.ERROR_MESSAGE);
}
}
}
===================================
Run the program and let me know if you have any issues.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.