Create a directory for this exercise and download the file CheckPassword.java to this directory. This file contains the skeleton of a small program to check the validity of a password. Your task is to add the missing code to this skeleton.
For a password to be deemed valid, it should have a length of at least
twelve characters, and it should contain at least two digits. These two
checks should be performed by the static methods longEnough()
and
atLeastTwoDigits()
, respectively. Each method should return a value
of true
if the required condition is satisfied, or false
if it is not.
The password to be checked must come from the command line used to invoke the program. You should NOT prompt for input of the password once the program is actually running. Consult the Java Tutorials for an explanation of command line arguments if you are not sure how they work. After checking the password, the program should print a short message (see below), informing the user whether it is valid or not.
If no password has been provided on the command line, the program should print a usage message (see below) to the standard error stream and then terminate with a non-zero exit status code.
Here are examples of expected program behaviour:
$ java CheckPassword jkft45aa
Password is not valid
$ java CheckPassword axhpqwcn99pd
Password is valid
$ java CheckPassword
Usage: java CheckPassword <password>
You will need to use methods of the String
class here – see
Section 2.3.3 of Eck’s book and the API documentation
for help with this.
Character type can be determined using the various static methods of the
Character
class. For example, Character.isDigit(c)
will return true
if the character c
is a digit, false
if it is not. See the
Character API documentation for further details.
Use System.err
to print the usage message. This can be used in the
same way as System.out
. Remember that System.exit()
can be used to
terminate a running program.
□