Due Tuesday, 23-Aug @ 8:00pm on Gradescope
In this assignment you’ll build a simple contact list, using data in names.txt
as an initial dataset. We’re not writing the next Microsoft Outlook here, but we’ll get a basic contact list up and running.
Download a pre-prepared Eclipse Project hw2.zip
and import it into Eclipse using these instructions. You will make all of your changes inside of Contact.java
and Contacts.java
.
For this assignment you have 5 submissions. You should write your own testcases while you work so that you don’t waste submissions. After you have used your submissions, you may continue to submit, but your submission will be penalized 4 points for every extra submission. (So, your 6th submission receives a -4, your 7th submission a -8, etc.)
There are two classes you will be working in: Contact
and Contacts
.
Contact
ClassThe Contact
class should represent a single contact. You need to fill-in the Contact
class with everything required to store and manage a single contact. You will need relevant instance variables, at least one constructor, and methods (including a toString
). The design of the object is up to you.
Contacts
ClassThe Contacts
class represents the contact list itself. You will need to write the following methods:
public Contacts(String filename)
This constructor takes a filename (a String), opens the file and, assuming it is openable, loads the contact list with data from the file.
While doing this, it needs to ensure that the contact list does not contain any entries with duplicate andrew IDs. If the file contains two or more entries with the same andrew ID, only the first one should be added to the contact list.
This should also ensure that more than MAX_CONTACTS are never added to the contact list.
filename
— The file containing the contacts to load
public boolean addContact(String firstName, String lastName, String andrewId, String phone)
Add a contact to the contact list. If there is room in the contacts array, verify that the new Contact has a unique andrew ID and, if so, add the contact after the current last element, updating numContacts
appropriately.
Parameters:
firstName
— The firstname of the contact
lastName
— The lastname of the contact
andrewId
— The andrewId of the contact
phone
— The phone number of the contact
Returns: True if the contact was successfully added and false otherwise
public void removeContact(String id)
Remove a contact from the contact list. If there is no contact with the requested andrewId then this method should do nothing. When removing the contact, other contacts in the array will need to be shifted to ensure there are no “gaps” in the array.
id
— The andrewId of the contact to remove
public boolean addContactToGroup(String id, String g)
Add a contact to a group.
If the specified id does not represent a current contact, then return false.
If a contact is already part of the specified group or the contact cannot be added to the group for any reason, then return false.
Note: A contact can be in, at most, 10 groups.
Parameters:
id
— The andrewId of the contact
g
— The group to add the contact to
Returns: True if the contact was successfully added to the group, and false otherwise.
public void changePhone(String id, String newNumber)
Change the phone number of a contact.
Parameters:
id
— The andrewId of the contact whose phone number should be changed
newNumber
— The new phone number
public String[] allInGroup(String g)
Returns an array containing all of the andrewIds for contacts in the contact list that are in the specified group.
Parameters: g
— The group whose contacts you want a list of
Returns: An array of andrewIds. The array should be the exact size of the number of andrewIds it contains.
public void updateDatabase(String filename)
Creates a file with name provided by fileName
and writes the contents of the contact list to this file. The file should follow the same file format as the input file. Your file must be written so that it can be read back in by your program. Note: When testing this, you will quickly discover that Eclipse doesn’t automatically show you new files that appear in a project. To force Eclipse to update the list of files in a project, right-click on the project and choose “Refresh”.
filename
— The name of the file to be written.
private int findContact(String id)
Optional: A helper function you should consider writing. Having it might help you with some of the above methods. You can probably figure out what it should do…
The methods you need to write are not necessarily presented in the order that you should write them. You should look through the entire skeleton file and think carefully about which methods to write in which order.
Hint 1: Make sure you can always test your new code as you write it.
Hint 2: Some methods can be used as helper functions for other methods. For example, addContact
might be useful to call from your constructor.
If you make a plan before you start coding, things will go faster and you will spend less time on the assignment.
The input (and output) files that your program should be able to work with have a comma separated value (CSV) format. Each line the file contains the information for one contact, with each field of that contact separated by a comma. The fields are:
First Name, Last Name, andrew ID, Phone #, Group 1, ..., Group N
Most of these fields are self explanatory. Groups 1 through N represent contact groups which this contact belongs to. Every contact will belong to at least one group, but may be in up to 10 groups.
The provided Eclipse project includes a sample file (names.txt
) that you can use for testing. (Looking at it will also help you make sure you understand the file format.)
There are multiple parts of the grading of this assignment:
For the first 90 points, your submission will be auto-graded on Gradescope.
For the next 5 points, your submission will be manually graded to check for good implementation methodologies. (Did you use a good approach to solving the problems?)
For the next 5 points, your submission will be manually graded to check for good testcases that you include in the main method. (Do you have 2-3 of your own testcases for each method, and do they all execute automatically?) There is one testcase already included to serve as an example.
Your code will also be checked for style. The parts of style that can be checked automatically (things like spacing, indentation, the use of CamelCase, etc.) are automatically checked by the autograder. Other parts of style, such as choosing good variable names, will be checked manually. Autograded style guide violations can result in, at most, -10 points. Manually checked style guide violations can result in, at most, -5 points.
You will submit your program to Gradescope. Login to the system and you will see the homework. Once there, you need to submit a zip
file containing your code. Lucky for you, however, Eclipse can create this zip file for you. Check out these instructions for exporting. On Gradescope, you’ll submit that exported zip
file. On the page that follows your submission you will see your live score (out of 90). It may take up to a few minutes for your score to appear; during that time just hit refresh in your browser. If you receive a lower score than you expect, you can click on the score to see the feedback from the autograder.
You must follow the model of our testcases from previous assignments (meaning you print when you start, print the results (pass/fail) when you finish, etc.) Additionally, you must comment each testcase with a note describing what it tests.
You may only use the following Java libraries in this assignment:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Arrays;
If you have questions, please post to Piazza. The course staff, including the instructor, monitor and answer questions there.
When posting to Piazza, if you include any code make sure your question is posted privately to the course staff, and not to the entire class.
Do not change the names of any of the provided methods or files. You may, however, add additional methods as needed.
After you submit to Gradescope, make sure you check your score. If you aren’t sure how to do this, then ask.
There is no partial credit on Gradescope testcases. Your Gradescope score is your Gradescope score.
Read the last bullet point again. Seriously, we won’t go back later and increase your Gradescope score for any reason. Even if you worked really hard and it was only a minor error…
You can submit to Gradescope multiple times. So, after you submit you should check your score. If it isn’t a 90 you can keep working until it is.
Don’t forget to make sure your code conforms to the style guide. We’ll be taking a look at that!
The style guide has a bunch of small rules with regards to indentation and spacing that are hard to follow perfectly. Luckily, Eclipse can handle it for you! Go to Source -> Format
in Eclipse and watch all of your spacing and indentation problems get fixed automatically. (You should do this before every submission.)
Don’t forget to write your own testcases! You are unlikely to succeed if you do not. (Remember, you are limited on submissions.)