12.3. Exercises

You can download the source code for the examples in the book and the solutions to the following exercises from http://www.wrox.com.

  1. Define a Person class to encapsulate a person's name and address with the name and address being fields of type Name and Address. Write a program to allow names and addresses to be entered from the keyboard and stored as Person objects in a file. Once the file exists new entries should be appended to the file.

  2. Extend the previous example to optionally list all the names and addresses contained within the file on the command line.

  3. Extend the previous example to add an index based on the person's name for each person entered at the keyboard to locate the corresponding Person object in the object file. The index file will contain entries of type IndexEntry, each of which encapsulates a name and a file position in the object file. The index file should be a separate file from the original file containing Person objects.

    Note: You will probably find it easiest to delete the previous file before you run this example so that the object file can be reconstructed along with the index file. You can't get the file position in an object stream in the same way as you can with a channel. However, you can use the sequence number for an object as the index—the first object being 1, the second being 2, and so on.

  4. Use the index file to provide random direct access to the object file for querying random names entered from the keyboard. Entering a name from ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.