need two files a test file with the main method and object for other file and th
ID: 674962 • Letter: N
Question
need two files a test file with the main method and object for other file and the other file need answer in 3-4 hours thanks
Write an application that accepts a user’s password from the keyboard. When the entered password has fewer than six characters, more than 10 characters, or does not contain at least one letter and one digit, prompt the user again. When the user’s entry meets all the password requirements, prompt the user to reenter the password, and do not let the user continue until th e second password matches the first one. Save the file as Password.java . Again, don’t forget to create the application/project Password Test .java Class that has the main method and an object to use Password class .
Explanation / Answer
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Password pd = new Password();
pd.readPassword();
}
}
class Password {
public void readPassword() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter password: (password should contain 6 to 10 characters only and contain at least one letter and one digit)");
boolean isValid;
String pwd= null;
do {
pwd = scan.nextLine();
isValid = validatePassword(pwd);
if(!isValid)
System.out.println("Enter a valid password");
} while(!isValid);
isValid = false;
do {
isValid = reEntrerPassword(scan, pwd);
} while(!isValid);
}
private boolean reEntrerPassword(Scanner scan, String pwd ) {
System.out.println("Re-enter password: ");
String repwd = "" ;
while(!pwd.equals(repwd)) {
repwd = scan.nextLine();
if(repwd != null) {
if(pwd.equals(repwd))
return true;
else {
System.out.println("passwords not matched. Re-enter matching password : ");
}
}
}
return false;
}
private boolean validatePassword(String pwd) {
//if password not empty
if(pwd != null && !"".equals(pwd.trim())) {
if(pwd.length() >=6 && pwd.length() <= 10) {
boolean containsdigit = false;
boolean containsletters = false;
for(char ch: pwd.toCharArray()) {
//check it contains digits or not
if(!containsdigit && Character.isDigit(ch)) {
containsdigit = true;
} else if(!containsletters && Character.isAlphabetic(ch)) {
//checking it contains letters are not
containsletters = true;
}
if(containsdigit && containsletters)
return true;
}
} else {
System.out.println("password should contain 6 to 10 characters only ");
}
}
return false;
}
}
---------------------------output---------------------------------------------
Enter password: (password should contain 6 to 10 characters only and contain at least one letter and one digit)
testp
password should contain 6 to 10 characters only
Enter a valid password
testpwd
Enter a valid password
test12
Re-enter password:
test123
passwords not matched. Re-enter matching password :
test12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.