Answer:
The Java code is given below with appropriate comments for better understanding
Step-by-step explanation:
import java.util.Scanner;
public class ValidatePassword {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a password: ");
String password = input.nextLine();
int count = chkPswd(password);
if (count >= 3)
System.out.println("Secure");
else
System.out.println("Not Secure");
}
public static int chkPswd(String pwd) {
int count = 0;
if (checkForSpecial(pwd))
count++;
if (checkForUpperCasae(pwd))
count++;
if (checkForLowerCasae(pwd))
count++;
if (checkForDigit(pwd))
count++;
return count;
}
// checks if password has 8 characters
public static boolean checkCharCount(String pwd) {
return pwd.length() >= 7;
}
// checks if password has checkForUpperCasae
public static boolean checkForUpperCasae(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isLetter(pwd.charAt(i)) && Character.isUpperCase(pwd.charAt(i)))
return true;
return false;
}
public static boolean checkForLowerCasae(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isLetter(pwd.charAt(i)) && Character.isLowerCase(pwd.charAt(i)))
return true;
return false;
}
// checks if password contains digit
public static boolean checkForDigit(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isDigit(pwd.charAt(i)))
return true;
return false;
}
// checks if password has special char
public static boolean checkForSpecial(String pwd) {
String spl = "!@#$%^&*(";
for (int i = 0; i < pwd.length(); i++)
if (spl.contains(pwd.charAt(i) + ""))
return true;
return false;
}
}