I need help making a program using three userdefined functio
I need help making a program, using three user-defined functions/methods for three rules
Suppose a website impose the following rules for passwords.
A password must have at least eight characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password. If the user enters a invalid password, the program needs to tell the user why it is invalid and ask the user to enter another password. The program should stop only when the user enters a valid password.
Here is one sample run to check these 4 passwords in order
 (My password18 à pass18 à password à Mypassword18):
Enter a new password: My password18
Invalid: Only letters and digits
Enter a new password: pass18
Invalid: Too Few Characters (8 at least)
Enter a new password: password
Invalid: At least two digits
Enter a new password: Mypassword18
Valid password!
The Problem Description, hints, deliverables are exactly the same as Project 3. The difference is that you need to define 3 methods and use these 3 methods to check the passwords. The three methods corresponding to the three rules you need to define are:
public static boolean AtLeast8(String p)
public static boolean LetterOrDigit(String p)
public static boolean AtLeast2Digits(String p)
Additional Hints:
These three functions need to be defined outside the main function block and inside the class block.
2.All three methods need to return true or false (boolean return type). true means the password can pass the rule while false means the password does not pass the rule. For example, to check the first rule (A password must have at least eight characters), we define the method as follows:
public static boolean AtLeast8(String p){
if (p.length() >= 8) return true;
else return false;
}
Solution
PasswordCheckerUsingMethods.java
import java.util.Scanner;
public class PasswordCheckerUsingMethods {
   public static void main(String[] args) {
        //Declaring variable
        String str = null;
       
        // Scanner object is used to get the inputs entered by the user
        Scanner sc = new Scanner(System.in);
       
        //This loop continue to execute until the user enters a valid password
        while(true)
        {
            //Getting the input entered by the user
            System.out.print(\"Enter a new password :\");
            str = sc.nextLine();
           
            /* checking whether the password contains letter or digit by calling the method
            * if yes,then this block of code get executed and display
            * error message and prompt the user to enter again
            */
            if (LetterOrDigit(str)) {
               
                //Displaying the error message
                System.out.println(\"Invalid: Only letters and digits\");
                continue;
               
                /* checking whether the password length is atleast 8 or not by calling the method
                * if yes,then this block of code get executed and display
                * error message and prompt the user to enter again
                */
            }else if(AtLeast8(str))
            {
                //Displaying the error message
                System.out.println(\"Invalid: Too Few Characters (8 at least)\");
                continue;
            }
            /* checking whether the password contains atleast 2 digits by calling the method
            * if yes,then this block of code get executed and display
            * error message and prompt the user to enter again
            */
            else if(AtLeast2Digits(str))
            {
                // Display the error message
        System.out.println(\"Invalid: At least two digits\");
        continue;
            }
            else
                System.out.println(\":: Valid Password ::\");  
            break;
        }
       
}
   /* This method will check whether the password contains
    * atleast 2 digits or not
    * Params : String
    * Return : boolean
    */
    private static boolean AtLeast2Digits(String str) {
        boolean digit=false;
        int count=0;
        for(int i=0;i<str.length();i++)
        {
            if (Character.isDigit(str.charAt(i))) {
    digit = true;
    // Counting the number of digits
    count++;
    }
    }
    /*
    * if the string does\'nt contain digit or if the count of digits
    * is less than 2 then if block will get executed
    */
    if (digit == false || (digit == true) && (count < 2)) {
    return true;
    }
    else
        return false;
           
        }
    /* This method will check whether the password length
    * is atleast 8 or not
    * Params : String
    * Return : boolean
    */
    private static boolean AtLeast8(String str) {
    if(str.length()<8)
        return true;
    else
        return false;
    }
   /* This method will check whether the password contains
    * only letters or digits or not
    * Params : String
    * Return : boolean
    */
    private static boolean LetterOrDigit(String str) {
        int flag=0;
        boolean bool = false;
        for (int i = 0; i < str.length(); i++) {
            // Checking whether the password contains any other thing
            // other than digit or character
            if (!Character.isDigit(str.charAt(i)) && !Character.isUpperCase(str.charAt(i)) && !Character.isLowerCase(str.charAt(i))) {
                flag=1;
 
            }
       
        }
        if(flag==1)
        return true;
        else
        return false;
    }
 }
_____________________________
output:
Enter a new password :My password18
 Invalid: Only letters and digits
 Enter a new password :pass18
 Invalid: Too Few Characters (8 at least)
 Enter a new password :password
 Invalid: At least two digits
 Enter a new password :Mypassword18
 :: Valid Password ::
______________Thank You




