Carnegie Mellon University Website Home Page
 
Fall 2013

15-121 Homework 2: Address Book - Due 9/16 at midnight

Download and unzip hw2-code.zip, which contains the versions of the Person.java and the AddressBook.java files that we developed in class on Thursday, with one exception. To make it easier (and comply with CMU's rules on release of personal information) I have changed the nationality field to andrewID! All the code that you write for this assignment should be added to one of these files.

Note: You will be graded in part on your coding style. Your code should be easy to read, well organized, and concise. You should avoid duplicate code.


Background: An Address Book

In this homework, you'll put the finishing touches on a primitive address book, using the data in names2.txt as a database. The data in this file consists of a person's first name, last name, andrew ID, age (I made this up) and phone number (made this up, too!). We're not quite ready to compete with Apple, but we should be able to get some reasonable functionality even at this stage of the course.


The Methods

You will need to write the following methods in the AddressBook class:

AddressBook(String fileName)
This constructor needs to be modified to work with the new data format in names2.txt.

String toString()
Needs to be modified to correctly return a properly formatted (spaced) String that represents the contents (used cells) of the address book.

void addPerson(Person p)
Assuming there is room in the address book, adds Person p after the current last element in the address book, updating numContacts appropriately.

void removePerson(String id)
Removes the entry for the person with andrew ID, id, from the address book, if it exists. All the entries after that person need to be moved up one slot in the array and the number of entries in the address book should be decreased by one. If there is no person with the andrew ID, id, the method should print a message to that effect.

void changeNumber(String id, int newNumber)
If a person with andrew ID, id, exists, changes the phone number that belongs to the person with that ID to newNumber. If no such person exists, the method should print a message to that effect.

void allWithAge(int age)
Prints all the entries in the address book with the specified age. Prints nothing if no entries match the specified age.

void updateDatabase(String fileName)
Creates a file with name provided by fileName and writes the contents (used cells) of the address book to this file.

Some of the methods you will write in the AddressBook class will require that you change/add methods in the Person class (e.g., removePerson will need a getID method to be added to the Person class). Others may require a different getter or setter.

Of course, you will also need to test your methods by calling them from main.


Submitting Your Work

When you have completed this assignment and tested your code thoroughly, create a .zip file with your work (including both files Person.java and AddressBook.java). Name the zip file "your-last-name".zip and email it to me mjs@cmu.edu.

Make sure to keep a copy of your work just in case something goes wrong!