Question
import java.util.Scanner; /* * Method validate() takes a password string and determines if it is a valid password * Valid passwords are at least 8
import java.util.Scanner;
/*
* Method validate() takes a password string and determines if it is a valid password
* Valid passwords are at least 8 characters in length and contain at least: one upper case letter,
* one lower case character, one digit, and one of 3 special characters from the set '!' '$' '#'
*/
public class QB4
{
/*
* Validate a password string. Returns true if the string is at least 8 characters in length and
* contains at least: one upper case character, one lower case character, one digit and contains one special
* character from the set of 3 special characters '!' '$' '#'
* returns false otherwise
*
*/
public static boolean validate(String password)
{
//-----------Start below here. To do: approximate lines of code = 17
// Fill in the method. Hint: make use of String method charAt(), class Character static methods:
// isLowerCase(), isUpperCase(), isDigit()
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
String password = "aBc123xyz!";
Scanner in = new Scanner(password);
if (validate(in.next()))
System.out.println("ok");
else
System.out.println("Invalid Password");
System.out.println("Expected:ok");
password = "aBcxyz";
in = new Scanner(password);
if (validate(in.next()))
System.out.println("ok");
else
System.out.println("Invalid Password");
System.out.println("Expected:Invalid Password");
password = "hello$";
in = new Scanner(password);
if (validate(in.next()))
System.out.println("ok");
else
System.out.println("Invalid Password");
System.out.println("Expected:Invalid Password");
password = "Hello123#";
in = new Scanner(password);
if (validate(in.next()))
System.out.println("ok");
else
System.out.println("Invalid Password");
System.out.println("Expected:ok");
}
}
Include screenshots of the output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
import javautilScanner Method validate takes a password string and determines if it is a valid passw...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started