Back to 111 Main Page

Mobius strip

Assignment 10

Task

Learn some of the methods of Java's String class while working a little more with objects.

Steps

We're going to write a class that represents the details of a simple user account on a computer system, including full name, first name, last name, initials, username, password, and email address.

Write a class named UserAccount with the following details:

Instance variables. Whatever you think you need, though they should all be private.

Static variable. One constant (that is, final) static field that holds the domain name of your imaginary system. (If you can't think of a domain, use ics.hawaii.edu.) For example:

  private static final String DOMAIN = "ics.hawaii.edu";

Constructor. You need a constructor that takes only the new user's full name. (If you want to write additional constructors, you may. But your class needs to work properly if only this one constructor is called.)

  • public UserAccount(String fullname)

Accessor methods. Here are the methods you need and descriptions of what they should do in an easily-pasted format. You may use the included /** javadoc */ if you wish. However, do this only if they accurately describe your finished methods!

  /**
   * Returns this user's complete name.
   */
  public String getFullName() {}

  /**
   * Returns only the user's first name.
   * That is, returns all characters up to the first space in their full name.
   * If there is no space in their name, then returns an empty string ("").
   */
  public String getFirstName() {}

  /**
   * Returns only the user's last name.
   * That is, returns all characters from the last space in their full name
   * to the end of their full name.
   * If there is no space in their name, then returns the whole name.
   */
  public String getLastName() {}

  /**
   * Returns this user's initials.
   * That is, returns the first letter of each word in their name,
   * taken together and capitalized, without dots between them.
   */
  public String getInitials() {}

  /**
   * A user's username consists of the first letter of each of their first
   * and middle name(s) (if any), followed by enough of their last name
   * to fill 8 characters.
   *
   * If their last name is short, then the username may be fewer than 8 characters.
   * In the unlikely event that they have more then 8 words in their name,
   * the username may be longer than 8 characters (implementation dependent).
   *
   * The username is all lowercase.
   */
  public String getUsername() {}

  /**
   * The user's email address is their username@DOMAIN.
   * That is, user.getUsername() + "@" + UserAccount.getDomain()
   */
  public String getEmailAddress() {}

  /**
   * Returns this user's current password.
   * (This method does not generate a new password each time.)
   *
   * The returned password is a string of 8 random characters,
   * including only lowercase letters, uppercase letters, and digits.
   * It does not include any punctuation.
   */
  public String getPassword() {}

  /**
   * Returns the domain name of the system hosting all UserAccounts.
   */
  public static String getDomain() {}

Note that only getFullName should return anything that contains spaces. Also, all these methods return a String; none of them print anything to the screen themselves.

Mutators methods. Since our system is going to construct usernames from users' full names, we don't want to let users change their name once they sign up for an account. (If they want to do that, they'll have to just delete their account and create a new one.) So the only mutator method we have is:

  /**
   * The new password passed to this method should contain at least one character
   * and should contain only lowercase letters, uppercase letters, or digits.
   * If it does not, this method will not use it to replace the current password.
   *
   * This method returns true if the new password was acceptable;
   * it returns false if there was a problem and the password for this account
   * was not updated.
   */
  public boolean setPassword(String newPassword) {}

You will need to use the methods of the String class to complete this assignment. Go to the Java API Documentation and have a look in the java.lang package for the String class. You may also want to check out the Character class too, especially the isLetterOrDigit method, which would be handy for the setPassword method.

Testing

If you like, you can also add a main method to your UserAccount class. In that method, you can write your own tests for your other UserAccount methods.

In addition, your UserAccount must compile with the following tester program: UserAccountCreater.java. (This verifies that you correctly matched the method signatures specified above.)

Sample Output

Here is some sample output from running UserAccountCreator:

D:\TA\hw\A10>java UserAccountCreator
You are creating an account on the ics.hawaii.edu system.
Enter your full name: Zachary Michael Tomaszewski

--- Your new account details ---
Full name: Zachary Michael Tomaszewski
First name: Zachary
Last name: Tomaszewski
Initials: ZMT
Username: zmtomasz
Email address: zmtomasz@ics.hawaii.edu
Password: d0k00bQt
Password (reconfirmed): d0k00bQt

Enter new password: 4superDuperSecret
Password changed.

D:\TA\hw\A10>java UserAccountCreator
You are creating an account on the ics.hawaii.edu system.
Enter your full name: rambo

--- Your new account details ---
Full name: rambo
First name:
Last name: rambo
Initials: R
Username: rambo
Email address: rambo@ics.hawaii.edu
Password: KACswkl6
Password (reconfirmed): KACswkl6

Enter new password: big gun!
Password not changed: "big gun!" is invalid.

Hints and Suggestions

  • You will need to use loops and conditionals within your methods to complete this assignment. For example, you will need to use a loop in order to generate initials when you don't now how many words might be in a full name.
  • Check out the sample code for Fullname in the FAQ below. But note that this code is insufficient for this assignment, even when the method names are the same. For example, Fullname's getFirstName and getLastName will crash if there is only one word in the name. Also, getInitials only grabs the first and last initials; it ignores any middle names. It also inserts dots, which you don't want.
  • Don't try to write everything at once and then compile. Instead, /* comment out */ everything in UserAccountCreator.java that you don't need, then slowly uncomment lines as you complete the necessary methods in your UserAccount.java. Test each method as soon as you write it before going on to write the next one.
  • Try to reuse your existing methods as much as possible. For instance, to create the username, you could first get the initials (by calling your own getInitials method), then remove the last character (using String's substring method), and then append the last name (produced by your own getLastName method). (Don't forget to then toLowerCase the constructed username String before you return it.) Once you've written getUsername, you can complete getEmailAddress in a single line.
  • Since you don't want to generate a new password every time getPassword is called, you should probably save a freshly generated password in an instance variable in the constructor when a new UserAccount is created. I recommend you write an extra method named generatePassword that will generate a random password for you. This can be a bit tricky, so here's how to generate a single random character or digit:
    	String password = "";
    	int random = (int) (Math.random() * (10 + 26 + 26));
    	if (random < 10) {  //add a digit
    	  password += random;
    	} else if (random < 36) { //add an uppercase letter
    	  password += (char) ('A' + (random - 10));
    	} else {  //add lowercase
    	  password += (char) ('a' + (random - 36));
    	}
    
    This code will append to the String password either a single digit (0-9), an uppercase letter (A-Z), or a lowercase letter (a-z). Now you just need to figure out how to repeat this 8 times (without pasting this code 8 times!) to build a password of the right length.

What to submit

Attach your UserAccount.java to an email.

FAQs

Do you have any examples of this sort of thing?
See Fullname.java for some examples of some of the String methods you'll need.

Also relevant:
  • PrintDiagonally.java -- Three different ways to print a string diagonally, showing the use of String's charAt method, loops, and that there's always more than one way to do it.
  • PrintOneWordPerLine.java -- For the getInitials method, you need to go through the name word-by-word. Here's some examples of different ways you might do that (though here the task is to just print out each word on its own line).
How do I check that the password is acceptable for the setPassword method?
You need to go through and check whether each character is acceptable. As mentioned above, there is a static method in the Character class named isLetterOrDigit that will tell you whether a single character is a letter (uppercase or lowercase) or digit. Here is a partial solution:
  for (int i = 0; i < newPassword.length(); i++) {
    char current = newPassword.charAt(i);
    if (!Character.isLetterOrDigit(current)) {
      return false; //password contains an invalid character
    }
  }
  //made it this far, so all characters must be okay
There is still more to do for this method, of course. You need to make sure that newPassword contains at least one character. If newPassword proves acceptable, you need to overwrite the current password and return true.

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Coding Standards
Follows required coding standards.
2 - Compiles
Runs with UserAccountCreator.java
1 - Constructor
Has the requested constructor.
4 - Accessor methods.
Contains the 8 requested accessor ("getter") methods, and they correctly perform as documented above
1 - Mutator method.
Has a correctly working setPassword method (as described above).


~ztomasze Index : TA Details: ICS111: Assignment 10
http://www2.hawaii.edu/~ztomasze
Last Edited: 11 Mar 2008
©2008 by Z. Tomaszewski.