| Project #4 | CMSC 131 |
| Due Tuesday 3/14/06 at 11:00 PM | Object-Oriented Programming I |
| Type of project: Closed | Spring 2006 |
Objective
To practice using the String class, writing classes from scratch, and writing JUnit test cases.
Overview
Note that this project is CLOSED. Please review the course policy on open/closed projects before you begin.
For this project you will write three classes: Name, Address, and Letter. You will also write an extensive set of JUnit tests, which should test all of the methods you have written in your classes. This project is different from most projects in this course, because there is no main() method anywhere. What you are creating are three re-useable modules (Name, Address, and Letter) that could later be made part of a complete program.
Put all of your work (Name.java, Address.java, and Letter.java) in the folder we have provided labeled "src".
Tests
Java API that will be helpful
Before beginning this project, you may want to look over the online documentation for the Java String class. You will need to make use of some of the methods you can read about there. You should also familiarize yourself with the static method called "isLetter", which is part of the Character class. You'll need it!
Writing the Classes
We are leaving many decisions about the implementation of these classes for you to decide. Below are descriptions of the classes you must write.
Name Class
The "state" for a Name object consists of two fields, described below. You may also include any static constants that you think are useful.
Private fields
- first name (represented as a String)
- last name (represented as a String)
The methods you must write are described below. As mentioned previously, we are intentionally leaving a lot of decisions about how to write these methods for you to decide. In particular, you must decide which of these things (if any) should be static. In addition to the methods below, please feel free to also include as many private methods as you find useful.
Public methods
- default constructor -- sets both first and last names to "Unknown".
- constructor with two String parameters -- the first parameter represents the last name, the second parameter represents the first name.
- constructor with one String parameter -- the parameter represents both the first and last names together, but separated by a comma. For example, if the parameter is "Smith, Bob", then the constructor must set the last name to "Smith" and the first name to "Bob". You may assume that there will always be a comma between the names, and exactly one space after the comma (and no other spaces or stray punctuation marks anywhere.)
- copy constructor -- a constructor with one parameter (a Name). Makes the current object a copy of the parameter.
- getFirst -- accessor for first name
- getLast -- accessor for last name
- setFirst -- takes a String parameter. This method will first verify that the name proposed is valid (by calling the isValidName method, described later). If the name is valid, sets the first name equal to the parameter; otherwise, sets the first name equal to null.
- setLast -- takes a String parameter. This method will first verify that the name proposed is valid (by calling the isValidName method, described later). If the name is valid, sets the last name equal to the parameter; otherwise, sets the last name equal to null.
Important: Anyplace the first and last name are set, the mutators above (setFirst and setLast) should be called. This includes the constructors that you are writing for this class.
- equals -- takes a Name parameter. Compares the current object to the parameter and returns type boolean. The two Name objects are considered equal if both the first names and the last names match. IMPORTANT: Ignore case distinction when comparing the names. In other words, "Smith" and "SmItH" are considered equal. Also IMPORTANT: You must check if the parameter is "null" -- if it is, then just return false.
- compareTo -- takes a Name parameter and returns an int. Compares the two Name objects (the current object and the parameter) using the usual system for alphabetizing names. I.e.: Alphabetize them based on the last name, and only consider the first name in cases where the last names are a tie. As with the equals method, ignore case distinction when making the comparisons. If the current object precedes the parameter, return a negative value. If the parameter precedes the current object, return a positive value. Return 0 if they tie. You may assume that the parameter is not null.
- toString -- returns a String in the format: <firstName> <lastName>, where there is exactly one space between the two names. For example, if the last name is "Smith", and the first name is "Bob", then the return value should be "Bob Smith".
Private methods
- isValidName(String nameRequested) -- returns true if the parameter represents a valid name. A name is considered valid if it consists of 20 characters or fewer and each character is an alphabetical letter (A through Z, either upper or lower case.)
Address Class
The "state" for an Address object consists of the following fields:
Private fields
- a name (use a Name object)
- a street address (use a String). An example might be: "455 Maple St."
- a city (use a String)
- a state (use a String). Here we'll store state abbreviations. For example "MD".
- a zipcode (use a String). Even though it's a number, we'll store it as a String. For example: "20742".
The methods you must write are described below. As mentioned previously, we are intentionally leaving a lot of decisions about how to write these methods for you to decide. In particular, you must decide which of these things (if any) should be static. In addition to the methods below, please feel free to also include as many private methods as you find useful.
Public methods
- constructor that takes three parameters: A String that represents the name (for example "Smith, Bob"), a String representing the street address (for example "737 Main Street"), a String representing the city, state, and zipcode. An example of the third parameter would be: "College Park, MD 20742". You may assume that the punctuation will be exactly as shown here -- a comma after the city, followed by a space. The state will always be two letters, the zipcode will always be 5 digits, and there will always be exactly one space between the state and the zipcode. There will never be any stray punctuation marks, and there will never be any extra spaces after the zipcode. The constructor must set the five fields for the current object.
- accessors for the five fields. You must use the names: getName, getStreetAddress, getCity, getState, getZip.
- an equals method -- takes an Address parameter. First check if the parameter is null. If so, return false. Otherwise, checks if all five fields of the current object match those of the parameter. You must ignore case distinction when doing the comparisons.
- a toString method. A typical return value will look like what you see below. Don't forget that you can use the character '\n' to indicate "new line".
Bob Smith
1234 Maple Street
College Park, MD 20742
Letter Class
The "state" for a Letter object consists of the following fields:
Private fields:
- An Address object representing who the letter is from
- An Address object representing who the letter is going to
- A String representing the "content" of the letter.
The methods you must write are described below. As mentioned previously, we are intentionally leaving a lot of decisions about how to write these methods for you to decide. In particular, you must decide which of these things (if any) should be static. In addition to the methods below, please feel free to also include as many private methods as you find useful.
Public methods
- A constructor that takes two Address parameters -- the first parameter is who the letter is from; the second parameter is who the letter is going to. Set the content to the String "Empty" (with no punctuation).
- A constructor that takes three parameters -- the first parameter is the Address of who the letter is from; the second parameter is the Address of who the letter is going to; the third parameter is the content.
- A method called "letterToSelf". This method has one Address parameter. It has a return value of type Letter. The method will return a Letter object with the following characteristics: The "from" address will be equal to the parameter. The "to" address will also be equal to the parameter. The content will be "This letter is from <firstName> to <firstName>." where <firstName> represents the first name of the person whose address was passed in as the parameter. Note that there is a period at the end of the content message. For example, if the name of the person in the address being used is "Bob Smith", then the content of the letter will be "This letter is from Bob to Bob.". Again, the method returns a Letter object.
A toString method. A typical return value will look like what you see below. Don't forget that you can use the '\n' character to indicate the end of a line.
FROM:
Bob Smith
7373 Maple Street
Beltsville, MD 20705
TO:
Jane Doe
97663 Main Street
Arlington, VA 22206
CONTENT:
Hello Jane, How are you?
Requirements
Grading
Your grade will be determined as follows: