Do not use arrays. Design and implement a Java program (name it Password). In th
ID: 3722999 • Letter: D
Question
Do not use arrays. Design and implement a Java program (name it Password). In the program, design a method (name it CheckPassword) that takes a password as a parameter, determines whether the password is valid or invalid using the following rules. Then it returns the outcome as boolean value (true or false).
The rules are:
- Password must include at least 10 characters
- Password contains only letters and digits
- Password must include at least 3 digits
Use three additional boolean methods, one for each stated rule above, to check if that rule is met or not. A boolean method returns a boolean value. A password is valid if all three rule methods return true values, otherwise the password is invalid. Design the program main method such that it allows the user to re-run the program with different inputs (as we did in the previous assignment using a sentinel loop structure). Document your code and organize the outputs properly using escape characters. In the main method, ask the user to enter a password and then display the judgement “Valid Password” or “Invalid Password”. Sample runs are:
Enter a password: CS5000/01
Entered password: CS5000/01
Judgment: Invalid Password
Enter a password: CS5000
Entered password: CS5000
Judgment: Invalid Password
Enter a password: CS5000Section01
Entered password: CS5000Section01
Judgment: Valid Password
Enter a password: MyOldK9Dog
Entered password: MyOldK9Dog
Judgment: Invalid Password
Explanation / Answer
import java.util.Scanner;
public class Password {
boolean CheckCharLen(String str) //Boolean Method to check whether the string length is greater or equal to 10
{
int count=str.length();
if(count>=10)
{
return true;
}
else
return false;
}
boolean CheckChar(String str) //Boolean method to check whether string contains only letter and digits
{
for(int i=0;i<str.length();i++)
{
if((str.charAt(i)>=48&&str.charAt(i)<=57)||(str.charAt(i)>=65&&str.charAt(i)<=90)||(str.charAt(i)>=97&&str.charAt(i)<=122))
{
continue;
}
else
return false;
}
return true;
}
boolean CheckDigit(String str) //Boolean method to check whether string has at-least 3 digits
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>=48&&str.charAt(i)<=57)
count++;
}
if(count>=3)
return true;
else
return false;
}
boolean CheckPassword(String str) //Boolean method to check whether the string is a valid password or invalid password
{
if(CheckCharLen(str)&&CheckChar(str)&&CheckDigit(str))
{
return true;
}
return false;
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
while(true)
{
Password ps=new Password(); //Creating a object of class Password
System.out.println("Enter the password: ");
String str;
str=sc.next(); //Getting the string as an input from user
System.out.print("Entered Password ");
System.out.println(str);
System.out.print("Judgement: ");
if(ps.CheckPassword(str)) //calling the CheckPassword method and verifying the password
{
System.out.println("Valid Password");
}
else
{
System.out.println("Invalid Password");
}
System.out.println("Enter -1 to end the program or enter 1 to continue"); //To continue please enter 1 or to end the progrma please enter -1
int d=sc.nextInt();
if(d==-1)
break;
else
continue;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.