Description
You will write a program this week that inputs a user’s first name, middle name or initial, and last name, then produces a user name and allows the user to set a password.
- UsernameGenerator.java
Requirements: Create a program that prompts for and reads the user’s first name, middle name (or initial), and last name separately. The program should then print a string composed of (1) the first letter of the user’s first name, (2) followed by first letter of the user’s middle name, (3) followed by the first and last letter of the user’s last name, (4) followed by a random number in the range 100 to 999. Notes: (a) Any missing letters in (1) – (3) should be replaced by the letter z; (b) the printed username should be in all lower case. Similar algorithms are sometimes used to generate usernames for new user. After the username is generated, the program should prompt the user to set up and confirm a password, which should be of type String. If the user fails to confirm the password, the program should print an appropriate message that contains the word “failed” as shown in Scenario 2 below. Remember that the password should be case sensitive.
Design: Your program will need to accept all values as type String. Your program output should be exactly as follows (replace everything in italics with your own words):
Scenario 1: if the passwords user types in match.
Line # | Program output |
1 2
3 4 5 6 7 8 9 |
Prompt the user’s first name:
Betty Prompt the user’s middle name or initial: A Prompt the user’s last name: Parker Prompt user for password: Y234U Prompt user to type in the password again: |
10 11
12 13 14 |
Y234U
Welcome Betty A Parker! Your username is bapr389 Your password is Y234U |
Scenario 2: if the passwords user types in don’t match.
Line # | Program output |
1 2
3 4 5 6 7 8 9 10 11 12 |
Prompt the user’s first name:
Betty Prompt the user’s middle name or initial: A Prompt the user’s last name: Parker Prompt user for password: Y234U Prompt user to type in the password again: Y234Y
Failed to set up the new user’s login information. |
Code: Your program should use the nextLine method of the Scanner class to read user input. Note that this method returns the input as a String. See the String class in the Java API for details on methods such as: length, charAt, toLowerCase, equals, substring, etc. An empty string (i.e., a String with length 0) or a space character should be treated as a missing letter and replaced with the letter z as appropriate. You have two choices for generating random numbers: the Math.random method and (2) the java.util.Random class which has methods for returning a random numbers. See the Java API documentation (Math class and the Random class) for details. Section 3.4 in the text has examples of using the Random class and its methods. With either approach, you’ll need to think of a way to ensure that the random numbers are in the range of 100 to 999 (e.g., adding an offset).
Test: You should test several users with different names. Make sure that the letter portion of the username generated from your program follows the requirements above and the numbers in the username are random numbers. In addition to the scenarios above, consider the letters that would be used for following names and letters:
Name Entered | Letters |
Jack Albert Smith | jash |
A B C | abcz |
A B | abzz |
A C | azcz |
(nothing or spaces) |
zzzz