15-121 SPRING 2010 [CORTINA]

LAB 1

Download and open a copy of the PhoneDirectory project discussed in lecture: PhoneDirectory.zip

In this program, the PhoneDirectory class contains an array to hold the directory entries and an integer to indicate how many cells of the array contain directory entries. The directory entries are stored sequentially in the array starting from position 0 with no empty cells. So if there are 6 entries in the array, they are stored in position 0 through 5.

Read through this class to understand how it works.

To run the program, execute the PDApplication class. (In Eclipse, click on that class in the Package Explorer and then choose Run > Run As... > Java Application.)

EXERCISES

  1. Add a new private helper method named getDirectory for the PhoneDirectory class that returns a single string that contains ALL of the names and phone numbers in the phone directory in the order that they are stored in the array. The string, if printed, should look like:

    NAME1 / NUMBER1
    NAME2 / NUMBER2
    etc.

    Call this method in the addOrChangeEntry method after you modify the phone directory array and print the returned string to the Console (System.out). Run the program and add some phone entries to check your work.

  2. Implement the removeEntry method of the PhoneDirectory class so that if the name is present in the phone directory, all subsequent phone entries in the array are shifted up one position to fill in the gap left by the removed entry. Be sure to decrement the field that stores the number of entries if you find the given name. Test your implementation carefully.

    NOTE: The return type is String. You need to return the removed phone number for the application to function properly. If no phone entry is removed, return null instead.

  3. Add a method to the PhoneDirectory class to reverse the order of the directory entries in the array without creating a new array (we call this working "in place" to conserve memory). Once you do this, go to the PDConsoleUI and PDGUI classes and determine what needs to be changed to allow the user to use your new method. Make the changes in both classes and test thoroughly to see that your new method works as intended.

HANDIN

If you worked with another student, put both of your names in a comment at the beginning of your program. At the end of recitation, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1. (If you worked together, you only have to submit one program.)

FUTURE WORK: FUN STUFF FOR LATER

  • Modify the program so that phone numbers can only include digits.

  • Modify the program so that only 10 digit phone numbers can be stored in the directory (area code + 7-digit local number).

  • Modify the program so each directory entry can hold three phone numbers (home, work, cell). Remember that the phone directory and other classes would need to be modified as well to allow users to specify any of the three phone numbers now.