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

Problem Description: Some websites impose certain rules for passwords. Suppose p

ID: 3741242 • Letter: P

Question

Problem Description: Some websites impose certain rules for passwords. Suppose password rules are as follows: 1. A password must have at least eight characters. 2. A password consists of only letters and digits. 3. A password must contain at least two digits. Write a program with class name CheckPassword that prompts the user to entera 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. public static boolean AtLeast8(String p) public static boolean LetterOrDigit(String p) public static boolean AtLeast2Digits(String p) 4. Create a method AtLeast8 outside the main method with the header provided in For example, method AtLeast8 will return true if there are at least eight characters in the password otherwise it will return false. the problem description. The password is passed to the string variable p in this method. Inside the method do the following. Here are four sample runs: i) If the length of p is less than 8 then return false, otherwise return true. Enter your password: My password18 Invalid password Sample 2 Enter Invalid password your password: pass18 Sample 3 Enter your password: password Invalid password Sample 4 Enter your password: password18 Valid password You can use the following steps in your code: System.out.print"Enter your password:");

Explanation / Answer

import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CheckPassword { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter Password: " ); String password = scan.nextLine(); if(atLeast8(password) && letterOrDigit(password) && atLeast2Digit(password)) System.out.println("Valid Password"); else System.out.println("Invalid Password"); } public static boolean atLeast8(String p){ //Check if string is not null and has atleast 8 characters return (p!=null && p.length()>=8)? true: false; } public static boolean letterOrDigit(String p){ //REgEx to match only alphanumeric Pattern passPattern = Pattern.compile("^[a-zA-Z0-9]*$"); Matcher matcher = passPattern.matcher(p); if(matcher.find()){ return true; }else return false; } public static boolean atLeast2Digit(String p){ // RegEx match minimum of 2 digits Pattern passPattern = Pattern.compile("[0-9]{2,}"); Matcher matcher = passPattern.matcher(p); if(matcher.find()){ return true; }else return false; } } /*Output Enter Password: sajjvino Invalid Password Enter Password: pass18 Invalid Password Enter Password: password Invalid Password Enter Password: password18 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