created 12/03/99

# Programming Exercises

These exercises assume that your have read the phone book example in this chapter. All of the programming exercises are modifications of that program. Start by copying that program from the chapter into Notepad.

## Exercise 1 --- User Interaction.

Modify the program so that it asks the user for the person to look up:

```Name?  Violet Smith
The number is: (312) 223-1937

Name?  James Barclay
The number is: (418) 665-1223

Name?  Scott Eccles
Name not found.

Name?  quit
good-by
```

Of course, the user must enter the name exactly for a match to be found. To make things a little easier, use the `toUpperCase()` method of class `String` to convert the target name and each array name to upper case when equality is being tested. Now the name the user enters need not match upper and lower case exactly.

Click here to go back to the main menu.

## Exercise 2 --- Improved Interaction.

It is awkward that the user has to enter the full name. Modify the `PhoneEntry` class so that it contains `firstName`, `lastName`, and `phone`. Now the program asks for both last and first name. If the user enters just the last name, the program goes through the entire array, printing out every match. If the user enters both first and last names, the program will print out the first match, then stop.

```Last Name?  Smith
First Name? Violet
The number is: (312) 223-1937

Last Name?  Smith
First Name?
John Smith: (812) 339-4916
Violet Smith: (312) 223-1937
Willoughby Smith (312) 992-8761

Last Name?  quit
good-by
```

To fully demonstrate your program you should increase the size of the array and add more names and numbers.

Click here to go back to the main menu.

## Exercise 3 --- Adding and Deleting Names

Modify the program so that it can deal with an array that has null in some cells. Now alter the program so that the user has a choice of three actions:

1. Search for a name (as above).
2. Add a new name and phone number to the array.
3. Delete a name (and phone number) from the array.

To add a new name and number to the array, first look for a cell that contains null. Then construct a new `PhoneEntry` object and assign its reference to that cell. If no cells contain null report an error (but don't exit the program.)

(Simple Method:) To delete a name and number from the array, first find its cell, then assign null to that cell. (The `PhoneEntry` previously referenced by that cell will be collected by the garbage collector.) If the name to delete is not in the array, report an error.

(Better Method:) As it now stands, the program must deal with an array that might have null values scattered throughout its cells. This is awkward and for a large array is inefficient. A better notion is to keep the array organized so that all the nulls are together at the end. Now when the array is searched, the first null signals the end of useful data and the search stops.

To delete a name and number from such an array, first find the name's cell. If the name to delete is not in the array, report an error. Now copy the reference in the last non-null cell to the deleted name's cell. Set the last non-null cell to to null. Now the deleted `PhoneEntry` is garbage, and all the array still has all the nulls at the end.

Click here to go back to the main menu.

End of the Exercises