Carnegie Mellon University Website Home Page
 
Fall 2014

15-121 Homework 2: Simple Contact List - Due 9/15 at midnight

Download and unzip hw2-code.zip, which contains slightly modified versions of the Person.java and the Contacts.java files that we developed in class on Thursday, with one exception. To make it more realistic (and comply with CMU's rules on release of personal information) I have changed the nationality field to andrewID and added some data that we talked about in class! All the code that you write for this assignment should be added to one of these two 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: A Contact List

In this homework, you'll put the finishing touches on a primitive contacts list, 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, phone number (made up most of these) and "group". 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 Contacts class:

Contacts(String fileName)
This constructor needs to be modified to work with the new data format found 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 contacts lists.

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

void removePerson(String id)
Removes the entry for the person with andrew ID, id, from the contact list, 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 contact list should be decreased by one. If there is no person with the andrew ID, id, this 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 allInGroup(String g)
Prints all the entries in the contact list with the specified group, g. Prints nothing if no entries match the specified group. You will need to modify the Person class to add a method that returns a person's group in order to do this.

void updateDatabase(String fileName)
Creates a file with name provided by fileName and writes the contents (only the used cells) of the contact list to this file. Your file must be written so that it can be read back in by your program.

Some of the methods you will write in the Contacts 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 (as in allInGroup).

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 Contacts.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!