Homework #4 CMSC 131
Due Friday March 11, 6:00 pm Object-Oriented Programming I
Type of homework: Closed Spring 2005


In this homework you will practice defining classes and testing modules for classes you define.


For this assignment you will implement a class called Name which represents a person's name. The class implements several methods for the manipulation of Name objects. More details about the class are provided in the Specifications section. You may want to take a look at the Sample Run section before you read the detailed description. It presents a driver program (and expected output) that makes use of the Name class. This homework is considered a closed homework, therefore no student collaboration is allowed. Please see the Open/Closed Policy on the class web page.

In addition to the program, you must submit a testing class, and a time log.

This homework will be graded as follows:


Code Distribution

You can access the files associated with this homework by checking out the project labeled p4. In the distribution you will find:

Name Class


The goal of this project is for you to provide a full implementation of the Name class. This class represents a person's name by keeping track of the first name and last name of a person. For simplicity we are assuming no middle names are used.


Instance Variables

The Name class defines two private String reference variables, firstName and lastName, which represent a person's first and last name, respectively.

Static Constants

The class defines the following constants:

Name Type Access Specifier Value
TAG_FORMAT int public 1
COMMA_FORMAT int public 2
PLAIN_FORMAT int public 3

Private Non-Static Methods

Public Non-Static Methods

Public Static Method

In the following description <firstName> and <lastName> represent the instance variables associated with a Name object. The symbol <blank> is used to represent a blank character. The possible format choices are:

Requirements (Read Carefully)

Hint: The following methods from the String class may be helpful for this project: trim, indexOf, substring.

Submission (Please Read)

Sample Run

The following is a sample run of the Driver class that relies on the Name class you must implement. The Driver is provided along with the expected output. Keep in mind that the submit server tests don't use this driver. This Driver is to help you understand the functionality associated with the different methods of this class.


public class Driver {
	public static void main(String[] args) {
		System.out.println("Creating some objects");
		Name nameOne = new Name();
		String presidentOne = "George Washington";
		Name nameTwo = new Name(presidentOne);
		System.out.println("\nDifferent Formats");
		System.out.println(Name.Format(nameTwo, Name.TAG_FORMAT));
		System.out.println(Name.Format(nameTwo, Name.COMMA_FORMAT));
		System.out.println(Name.Format(nameTwo, Name.PLAIN_FORMAT));
		System.out.println("\nSetting some values");
		nameOne.setFirstName("  TOmas ");
		nameOne.setLastName("  Jefferson ");
		System.out.println("\nComparing some objects");
		if (nameOne.equals(nameTwo)) {
			System.out.println("First comparison: same name");
		} else {
			System.out.println("First comparison: different names");
		if (nameTwo.equals("  George  WashingTON  ")) {
			System.out.println("Second comparison: same name");
		} else {
			System.out.println("Second comparison: different names");
		System.out.println("Relative comparison: " + nameOne.compareTo(nameTwo));


Creating some objects

Different Formats
FirstName: George LastName: Washington
Washington, George
George Washington

Setting some values
TOmas Jefferson

Comparing some objects
First comparison: different names
Second comparison: same name
Relative comparison: -1

Challenge Problem

Remember that you are not required to implement the following problem. Please visit the course web page for information regarding challenge problems.

The challenge problem for this homework consists of modifying several methods of the Name class. You could add any of the following alternatives for the corresponding number of gold stars. Remember that your new "Name" class must work with the driver we have provided and with the set of tests available in the submit server. If you implement the challenge project then add a note to the time log indicating which of the gold stars option you have implemented.

One Gold Star

Modify the validName method so it can detect any invalid names. Invalid names could be: providing only one name and empty string.

Two Gold Stars

In addition to the requirements for one gold star, modify the Name class so it can handle middle names.

Three Gold Stars

In addition to the requirements for two gold stars, modify the Name class so it can handle any name extensions. For example, in John K. Smith Jr. "Jr." is an extension. Another kind of extension you could have is a roman numeral (e.g., John K. Smith III).