163k views
2 votes
Assume we have already defined a variable of type String called password with the following line of code: password' can have any String value String password ???"; However, because of increased security mandated by UCSD's technical support team, we need to verify that passwo rd is secure enough. Specifically, assume that a given password must have all of the following properties to be considered "secure": It must be at least 7 characters long .It must have characters from at least 3 of the following 4 categories: Uppercase (A-Z), Lowercase (a-z), Digits (0-9), and Symbols TASK: If password is secure, print "secure", otherwise, print "insecure" HINT: You can assume that any char that is not a letter (A-Z, a-z) and is not a digit (0-9) is a symbol. EXAMPLE: "UCSDcse11" would be valid because it is at least 7 characters long (it's 9 characters long), it has at least one uppercase letter (U', 'C', 'S', and 'D'), it has at least one lowercase letter (c', 's', and 'e'), and it has at least one digit (1' and '1') EXAMPLE: "UCSanDiego" would be invalid because, while it is 10 characters long, it only has uppercase and lowercase letters, so it only has characters from 2 of the 4 categories listed Sample Input: UCSDcse11 Sample Output: secure

User Spitz
by
5.0k points

1 Answer

5 votes

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;

}

}

User Samit Kumar Patel
by
6.1k points