Problem 4.2

CIS 210 Home Page Last updated 2007/10/17 21:28:08

Problem 4.2 - Coding a simple object

In this problem you are to code a simple object that has a constructor and whose only behavior is that it can be printed as a String.

Requirements of the object

When you sign up for a computer account or a web account, you often are given a user ID for logging in. A completely random string could be generated as your ID, but a better algorithm would generate something that is easier to remember. Using just your name is likely to collide with other users that have the same name, so we would like to address that issue as well, and have come up with the following requirements for the generated user ID:

This will result in a user ID that is at most eight characters. The number part of the ID should be randomly generated.

Driver code for the class

Driver code to test the class you write is in TestUserID.java. This driver has a main method that prompts for the name. The driver then creates a UserID object with these values. The only other thing the driver code does is to print the object out.

Coding the UserID class

Your job is to code a UserID class that will work with the driver code and conform to the requirements. The class must be named UserID and in a file named UserID.java.

Examples of running your code are:


C> javac TestUserID.java UserID.java
C> java TestUserID
Enter name: David Atkins
The UserID is: datki713
C> java TestUserID
Enter name: Thomas Wu    
The UserID is: twu97
Notice that in the driver code, there are no explicit method calls using the UserID object. So what are you supposed to code?

Remember that the creation of an object means that a constructor is called. We can see from the way that the UserID object is created that there must be a constructor taking two String's as arguments, so you will have to code that.

How does the UserID object get printed out? Remember that every object in Java has a method that provides a String representation so that objects may be printed out. You must code this method as well as the constructor.

You may define class instance data to hold the generated ID. In particular, if the object is printed repeatedly, it should produce the same ID. Only if a new object is created should you see a different ID.

Testing your solution

Test your solution with various values. Remember to consider borderline cases. Your code should work correctly (i.e., not produce an exception), even if the last name is shorter than five characters, or if the first name is the empty string.

Turn in

Your working version of UserID.java.