15-121 Homework 2: Simple Contact List - Due 2/5 at midnight
Download and unzip hw2-code.zip, which contains the
file Contact.java (based on the Person.java class we used in
lacture and a slightly modified Contacts.java that we developed in
class on Tuesday. To make it more realistic I have changed the
nationality field to andrewID and added some data that would normally be found
in a contact entry. 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 names.txt as an initial dataset. The data in
this file consists of a person's first name, last name, andrew ID, phone
number (made up most of these), zipcode, 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 names.txt. It also needs to make sure that everyone's andrew ID is
unique and report an error if a duplicate is found while reading the file.
String toString()
Returns a properly formatted (spaced) String that represents the used cells of
the contacts array. You will also need to create a toString() for
the Contact class.
boolean addContact(Contact p)
If there is room in the contacts array, verifies that the new Contact
has a unique andrew ID and, if so, adds Contact p after the
current last element, updating numContacts appropriately, and
returns true. If not, returns false.
void removeContact(String id)
Removes the entry for the contact with andrew ID, id, from the contact
list, if it exists. All the entries after that contact need to be moved up one
slot in the array and the number of used entries in the contact array should be
decreased by one. If there is no contact with the andrew ID, id, this
method should print a message to that effect.
void changePhone(String id, String newNumber)
If a contact with andrew ID, id, exists, changes the phone number that
belongs to the contact with that ID to newNumber. If no such contact
exists, the method should print a message to that effect.
void allInGroup(String g)
Prints all the entries in the contact array that are in the specified group,
g. Prints nothing if no entries match the specified group. You will
need to modify the Contact class to add a method that returns a
contact's group in order to do this.
void allInZip(int zip)
Prints all the entries in the contact array that have the specified zipcode,
zip. Prints nothing if no entries match the specified group. As above,
You will need to modify the Contact class to add a method that
returns a contact's zipcode 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. N.B.,
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 Contact 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 the assignment and tested your code thoroughly,
create a .zip file with your work (including both Contact.java
and Contacts.java). Name the zip file "your-andrew-id".zip and upload
it to the HW2 folder on Box.
Make sure to keep a copy of your work just in case something goes wrong!