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

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

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.

Exercise 4 — 3D Deathmatch

A three dimensional random walk is where a particle starts out at some location (X, Y, Z) then moves by small random increments in X, Y, and Z. Think of this as a confused firefly, fluttering randomly through the night.

Write a program that defines a Firefly class that has three instance variables, X, Y, and Z. The constructor for the class initializes each variable to a random value in the range -10.0 to nearly +10.0 representing the position of the firefly. Also, include an instance variable alive that is initialized to true. The class includes a move() method that adds random amounts in the range (-1.0, +1.0) to X, Y, and Z.

Create an array of 10 firefly objects, each initialized to a random location. Now go through the array in sequence moving each firefly object randomly. Each time a firefly moves, eliminate any other firefly that is a distance of 1.0 or less from the one that moved. Do this by setting alive to false. Continue until only one firefly is left. That firefly is the winner.

Print out interesting messages as all this is being done. (It would be nice to show graphics for this, but that would be a real project.)

Use the Pythagorean formula to calculate the distance between two objects:

distance = \/ (X1 - X2)2 + (Y1 - Y2)2 + (Z1 - Z2)2

Print out the number and final location of the winning firefly and the number of iterations it took.

Click here to go back to the main menu.

End of the Exercises