As part of your pre-lab for this week, write pseudocode to describe your plan to produce a user name based on a user's first and last name (consider those given), and a random integer (that you will produce).
A user name consists of:
You can assume that the user's last name has length at least 5.
Look at your lecture notes and textbook to identify a way (Java class and method) you can use to obtain the random integer needed.
This version does not need to deal with duplicate user names. (i.e duplicate user names would be acceptable).
You can start with any design you can come up with. But at the end, try to have the following methods :
Now produce some testing cases for this program. How many such cases do you need for this program?
Hand-Execute your pseudocode on two testing cases, (as we did in lab last week) assuming that the top-method, produceOneUserName(), is called first.
This time the sequence of execution won't be as simple as it was in the Voting program. Why?
Make sure to bring to lab your notebook containing your pre-lab work.
Now you should be able to write the java code to produce one user name, based on your work in the previous task.
Start a new project in BlueJ, name it lab2Project, and save it on your Desktop. Add a class, named UserNames1 (we will have more versions soon), to contain your code.
Add a main() method to test your code. In this version, hard-wire values for the first and last name.
Add a new class to your project, named UserNames2 to improve upon the previous version: Instead of hard-wiring values for the first and last names, you will ask the user for this information, and will use a Scanner to read it into your program.
Bring up your notes to refresh on how to set up a scanner, and use it to read Strings.
This program should need a rather small number of changes from the version 1 to get it done!
Add yet another class to your project, named UserNamesMany to produce many user names, not just one.
Your program should start by asking the user for their first and then their last name. Then it goes ahead and produces their username. At that point it should ask the user if they want to continue. Depending on the user's answer, the process continues or just stops.
Create a sketch of the process in pseudocode, on your notebook!
You will need to employ a loop here. Think about what kind of loop is more suitable in this case.
When you have a reasonable level of assurance that your plan is a good one, go ahead to write the java code.
Make sure to re-use code you have written in the previous task(s).
Here is an interesting extension/correction to this problem. You, most probably, won't have time to work on it during lab, but you may want to give it some though at a different time.
When we started, we made the assumption (pre-condition) that the user's last name is at least 5 characters long. One way to remove this pre-condition, would be to slightly change the rules when the last name is "too short" as follows:
When the last name contains less than 5 characters, use the whole last name, preceded by as many characters from the first name as you need to have 6 letters altogether. For example:
First name: Sherri Last name: Wang
The first component of the user name should be
shwang
====================================================================================
Tip:
As you start defining a new method, start by identifying its purpose. Remember, a method is trying to capture behavior for the object you're defining. So, should the method take any parameter? What should the method return? In the beginning, we will provide hints to help you in this decision-making process, but you will soon have to make these decisions by yourself. This power is one of the joys of programming (and with it, comes great responsibility, as we'll learn in the course).