| Homework #4 |
CMSC 131 |
| Due Friday March 11, 6:00 pm |
Object-Oriented Programming
I |
| Type of homework:
Closed |
Spring 2005 |
Objective
In this homework you will practice defining classes and testing modules for
classes you define.
Overview
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:
- 85% - Implementation (including 10% for style)
- 10% - Test class
- 5% - Time log
Specifications
Code Distribution
You can access the files
associated with this homework by
checking out the project
labeled p4. In the distribution you will find:
- Driver.java - Sample driver for the Name class and the one
presented in the Sample Run.
- timelog.txt
- JUnitName.java - The JUnit with the set of public tests available
in the submit server.
- MyTests.java - Shell for a class you must complete. This
class represents a Test class.
Name Class
Overview
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.
Variables
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 |
| DEFAULT_FIRST_NAME |
String |
private |
"NONAME" |
| DEFAULT_LAST_NAME |
String |
private |
"NOLASTNAME" |
| TAG_FORMAT |
int |
public |
1 |
| COMMA_FORMAT |
int |
public |
2 |
| PLAIN_FORMAT |
int |
public |
3 |
Private Non-Static Methods
- validName - This method takes a String as a parameter and has
a boolean return type. It determines whether the String parameter
represents a valid name. We consider a string valid only if it contains
letters and blank (' ') characters. Any number of blank characters
can exist before the first name, in between the first and last name, and
after the last name. If there are blank characters
in any other position, the name is invalid.
You can assume that there will always be a set of characters representing a
first name (even if it uses invalid characters) followed by at least one
blank character, followed by a set of characters representing the last name
(even it is uses invalid characters).
You can use the isLetter method of the Character class
to verify whether a character represents a letter.
- initFirstLastNames - This method takes two String references as
parameters and has a void return type. The first parameter represents
the first name and the second the last name. The method initializes
the firstName and lastName with duplicates of the String objects
represented by the parameters.
Public Non-Static Methods
- Default Constructor - Initializes the object with the
DEFAULT_FIRST_NAME and DEFAULT_LAST_NAME values.
- Additional Constructor - This method takes a String reference as a
parameter. This represents a person's full name. A person's full name is a first
name followed by a last name. The constructor will initialize the Name
object with the first and last names provided. The provided
string must be validated. If it does not represent a valid name, the
object will be initialized using the DEFAULT_FIRST_NAME and the
DEFAULT_LAST_NAME.
- Copy Constructor - This method duplicates the String objects
associated with the parameter Name object. The formal parameter for this
constructor is an object of the Name class.
- toString - Returns a string with the first name followed by the
last name with one blank character in between. You must use the Format method
to implement this method.
- setFirstName - This method has a void return type. It
updates the firstName instance variable with a duplicate of the parameter.
You can assume the parameter is a valid first name, however it can be
surrounded by any number of blank characters. Excess blank characters
will be removed by setFirstName.
- setLastName - This method has a void return type. It
updates the lastName instance variable with a duplicate of the parameter.
You can assume the parameter is a valid last name, however it can be
surrounded by any number of blank characters. Excess blank characters
will be removed by setLastName.
- getFirstName - Returns the firstName reference.
- getLastName - Returns the lastName reference.
- equals - This method has a boolean return type and a Name
reference as parameter. It returns true if the current object and the
parameter represent the same name. When comparing names,
case is ignored, which means "George Washington" and
"GEORGE wAsHiNGtOn" are considered to be the same name.
- equals - This method has a boolean return type and a String
reference as parameter. If the String does not represent a valid name
then false is returned.
Given a valid String, the method
will return true if the current object and the name represented by the
parameter are considered the same name and false otherwise.
Again, case is ignored, which means "George Washington"
and "GEORGE wAsHiNGtOn" are considered the same name.
- different - This method has a boolean return type and a Name
reference as parameter. It returns true if the current object and the
parameter represent different names. When comparing names,
case is ignored, which means "George Washington" and
"GEORGE wAsHiNGtOn" are considered to be the same name.
- different - This method has a boolean return type and a String
reference as parameter. If the String does not represent a valid name
then true is returned. Given a valid String the method will return
true if the current object and the name represented by the parameter
are different and false otherwise. Again, case is ignored, which means
"George Washington"
and "GEORGE wAsHiNGtOn" are considered the same.
- compareTo - This method returns an integer and takes a Name
reference as a parameter. This method allows us to compare two names.
The method will return -1 if the current object precedes the parameter in
alphabetical order, 0 if the current object and the parameter represent the
same name and 1 if the parameter follows the current object in alphabetical
order. Case is ignored, which means "George Washington"
and "GEORGE wAsHiNGtOn" are considered to be the same name.
Names are compared by first using the last name and then the
first name. For example, "Laura Jackson" will
precede "Kathy Smith".
Public Static Method
- Format - This method has a String reference as return type and two
parameters: a reference to a Name object and an integer. The method
will create a String object based on the provided Name reference and the
format specified by the integer value. You may assume the integer value input
is valid (no error checking).
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:
- TAG_FORMAT - FirstName: <firstName> LastName: <lastName>
- COMMA_FORMAT - <lastName>, <firstName>
- PLAIN_FORMAT - <firstName><blank><lastName>
Requirements (Read Carefully)
- You must not use the String split() method for this project.
- You may assume we will not pass a null reference for any method that
requires a reference parameter (e.g., no null references for equals).
- You must avoid code duplication. Code duplication can be
avoided if methods depend on other methods of the class to implement their
functionality. This project has been designed with this in mind.
As you implement the Name class think about how you can avoid code duplication
for each of the methods you must implement. You will lose
significant points if you duplicate code even if your implementation is
correct.
- You must use meaningful variable names and good indentation.
- You cannot add any additional classes beyond the ones specified above.
- Testing class - Testing your classes is a very important skill.
For this project you must complete the MyTests.java file with a set of
tests that verify the correctness of each of the methods you have
defined. As long as you have defined one test for each method you will
be fine. All the tests can be in one method or multiple methods.
That is up to you. The only restriction you have is that we must
be able to see the results of ALL your tests by just running the main() method of the
class. When visiting the TA during office hours we may ask you to
present this file to get help.
- You may not modify the prototypes (public/private, name, parameters,
return type) of any of the Name methods.
- Do not define any additional instance variables or methods beyond the ones
specified above.
- Do not use any new language constructs, e.g., arrays, that have not been
presented in class.
- Use the Eclipse
submit option
for your submission. If you are having problems with this, please contact
us.
Hint: The following methods from the String class may be helpful for this
project: trim, indexOf, substring.
Submission (Please Read)
- Immediately after reading this description, check out the code
distribution and submit. You need to make sure that you can submit your
project. Don't wait until the day before the project is due to submit
your project.
- Submit your project using the submit project option associated with
Eclipse. Remember to complete your time log before submitting your
homework.
- After you submit a project, you must verify it generates the expected
results by accessing the submit server.
The submit server has now being configured to test your program against a
set of tests. Some of these are are the ones available in the file
JUnitName.java. In addition you can ask for release testing, and secret
testing, as described in lecture. If you have doubts about the testing
environment we just described SEE A TA DURING OFFICE HOURS IMMEDIATELY. Do not wait
until the day the project is due as you may not be able to get the help you
need.
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.
Driver
public class Driver {
public static void main(String[] args) {
System.out.println("Creating some objects");
Name nameOne = new Name();
System.out.println(nameOne);
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(nameOne);
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));
}
}
Output
Creating some objects
NONAME NOLASTNAME
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).