Lab 8: Serialization
Program to write
Design an interactive address book. Your address book must, upon invocation:
- Read in any address data from a binary file of your choosing in
the current working directory, if the file exists;
- Display the number of addresses stored within the book;
- Provide the user a menu with options to
- Add a new entry,
- Delete an entry,
- Search for an entry or entries by last name, and
- Show all entries.
- Provide the user a way to exit, and, upon that selection,
- Serialize the entire address book to a file in the current directory.
Do not be overly concerned with what data fields you choose to represent in
your book. First name, last name, and street address are sufficient for our
Do implement Serializable in whatever class you write to implement
an individual entry in the book. (Vectors themselves are already
serializable.) You might want to read this
tutorial to familiarize yourself with serialization in java.
Also, create a sample database for your program that has at least five names,
of which at least two have the same last name. Include this with your
submission in your
- Allow your program to take a single command-line argument which is the
name of the database to use; if none is provided, the program should start a
new database. If one is provided, the program should load up that database
at the beginning of the program run.
- A text-based interface is sufficient for this lab, though you may find it
more natural to build a simple GUI. If you choose the latter, you may find
some or all of these classes to be useful:
JTextField class for entering values (also see
JTextArea class for displaying data (also see
- Alternatively, you can use the JTable
class for both entering and displaying data (also see this
You should also browse the Java Swing tutorials to see what other graphical
classes (often known as widgets) are available. Feel free to use any
that are useful to you.
WARNING! It is extremely easy to get carried away when designing a GUI
application and end up writing a much bigger program than you intended. If
you have the time to do this, great, but if not, don't worry about it. Using
JTables effectively, in particular, is a lot of work. The GUI should be
functional, but it doesn't have to be beautiful.
- We are deliberately not telling you what classes to write or how
to organize your program. In addition, the above specification doesn't
describe every detail of how the program should work (because real
specifications never do either). This lab should therefore be thought of
mainly as a design exercise. Designing programs well is one of the most
important aspects of programming. A bad algorithm can usually be fixed by
modifying only a small amount of code, but a bad design often can't be fixed
without completely rewriting the program (which is why software project
failures are often so spectacular).
Keep these things in mind:
- Write your program to behave in a reasonable and useful manner given the
rough specification given above. Ask yourself what kind of behavior you
would want if you were the user of the program, then implement that behavior.
- Handle errors in a reasonable manner. Don't just catch them and ignore
them. In particular, don't just have the program die with e.g. an
NumberFormatException if invalid input is entered.
- Break your program up into classes in a reasonable manner, with each
class performing a well-defined function. Don't stuff all the functionality
into a single class.
Question to answer
Answer the following in a comment block at the top of your code: Imagine if,
after you've built up a large address book on disk, you decide to add an
extra field to your entry class (e.g. you want to add a phone number
field). What happens if you change the code, recompile, and run your
program? Why? Don't just guess; try it and see.