This is done in Java. Imagine you are developing a software package for Amazon.c
ID: 3878181 • Letter: T
Question
This is done in Java.
Imagine you are developing a software package for Amazon.com that requires users to enter their own passwords. Your software requires that users’ passwords meet the following criteria:
The password should be at least six characters long.
The password should contain at least one uppercase and at least one lowercase letter.
The password should have at least one digit.
Write a class that verifies that a password meets the stated criteria. Demonstrate the class in another program that allows the user to enter a password and then displays a message indicating whether it is valid or not.
TIP: You will have two Java programs, one is definition, PasswordVerifier.java for instance, another one is demo, PasswordDemo.java for instance. In your PasswordVerifier.java program you will have to define methods, for example, hasUpperCase(), hasLowerCase(), hasDigit(), and isLongerThenSix() to verify the above mentioned criteria. In your PasswordDemo.java program, you will ask the user to input a password, then call each method defined in PasswordVerifier.java to check if the customer entered password is valid.
Explanation / Answer
import java.io.*;
import java.util.*;
class PasswordVerifier {
private String password;
public PasswordVerifier(String a){
password = a;
}
public boolean verify(){
boolean upper,lower,digit;
upper = false;
lower = false;
digit = false;
if (password.length() < 6)
return false;
for (int i=0; i<password.length(); i++){
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
lower = true;
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
upper = true;
if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
digit = true;
}
if (upper && lower && digit)
return true;
else
return false;
}
}
public class DemoVerifier{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter password: ");
String pass = sc.next();
PasswordVerifier pv = new PasswordVerifier(pass);
if (pv.verify())
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.