Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Create three boolean variables isAtLeast8, isLetterDigit, and isAtLeast2Digit

ID: 3740529 • Letter: 2

Question

2. Create three boolean variables isAtLeast8, isLetterDigit, and isAtLeast2Digits and Project: Check Password Problem Description assign the output of the corresponding methods to these boolean variables. For example, boolean isAtLeast8-AtLeast8(password) Some websites impose certain rules for passwords. Suppose password rules are as follows: 3. If each boolean variable is true then display Valid password otherwise display Invalid password. 1. A password must have at least eight characters. 2. A password consists of only letters and digits. /Method AtLeast8 3. A password must contain at least two digits. 4. Create a method AtLeast8 outside the main method with the header provided in Write a program with class name CheckPassword that prompts the user to enter a password and displays Valid password if the rules are followed or Invalid password otherwise. Create three methods (headers given below) to check the three rules. the problem description. The password is passed to the string variable p in this method. Inside the method do the following. i) If the length of p is less than 8 then return false, otherwise return true. public static boolean AtLeast8(String p) public static boolean LetterOrDigit(String p) public static boolean AtLeast2Digits (String p) //Method LetterOrDigit 5. For example, method AtLeast8 will return true if there are at least eight characters in the password otherwise it will return false. Create a method LetterOrDigit outside the main method with the header provided in the problem description. The password is passed to theigvariable p in this method. Inside the method do the followings. Here are four sample runs: i) Create a loop that checks whether each character in p is a letter or a digit. To Character.isLetterOrDigit p.charAtindex) The method isLetterOrDigit returns true if p.charAt(index) is a letter or digit. check this the in-built methods Sample 1: Enter your password: My password18 Invalid password ii) If a character is not found to be a letter or digit then return false inside the loop, otherwise return true outside the loop. Sample 2: Enter your password: pass18 Invalid password //Method AtLeast2Digits Sample 3: Enter your password: password Invalid password 6. Create a method AtLeast2Digits outside the main method with the header provided in the problem description. The password is passed to the string variable p in this method. Inside the method do the followings. i) Create an integer variable count and assign zero to it. Sample 4: Enter your password: password18 Valid password ii) Create a loop that counts the number of digits in p. The in-built method Character.isDigitip.charAtindex)) returns true ifp.charAt(index) digit. Increase count by one when a digit is found. ?s a You can use the following steps in your code: System.out.print Enter your password: "; iii If the count is at least 2 then return true inside the loop, otherwise return false outside the loop. 1. Store the user entry in String variable named password using nextLine method.

Explanation / Answer

CheckPassword.java

import java.util.Scanner;

public class CheckPassword {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter your password: ");

String password = scan.nextLine();

boolean isAtleast8 = AtLeast8(password);

boolean isLetterDigit = LetterOrDigit(password);

boolean isAtLeast2Digit = AtLeast2Digit(password);

if(isAtLeast2Digit&&isAtleast8&&isLetterDigit) {

System.out.println("Valid password");

} else {

System.out.println("Invalid password");

}

}

public static boolean AtLeast8(String s) {

return s.length()>=8;

}

public static boolean LetterOrDigit(String s) {

for(int i=0;i<s.length();i++) {

if(!Character.isLetterOrDigit(s.charAt(i))) {

return false;

}

}

return true;

}

public static boolean AtLeast2Digit(String s) {

int count = 0;

for(int i=0;i<s.length();i++) {

if(Character.isDefined(s.charAt(i))) {

count++;

}

}

return count>=2;

}

}

Output:

Enter your password:
password12
Valid password

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote