CIT 594, Notes on the Animals Assignment
Spring 2002, David Matuszek

If you have wondered about the ClassNotFoundException that could occur when you try to read in a binary tree from a file, it's very simple: You can only read in objects if you have a class that describes those objects.

For example, if you try to read in an object from your animals.dat file, and it turns out to be an object of type Employee, you would get this exception. (Unless, that is, you actually have an Employee class in your Animals program!)

A ClassNotFoundException can also occur if you write a binary tree to animals.dat, change the definition of your binary tree class, and then try to read in the old binary tree using the new definition.

Here's a problem you may not have thought of. Suppose that each time you add an animal to the binary tree, you also add it to your hash table. Then you write out the binary tree to animals.dat and quit the program. The next time you start the program and read in animals.dat, you no longer have the hash table, and your program doesn't know which animals are already in the binary tree. What do you do about this?

There are two obvious solutions:

  1. When you write the binary tree to animals.dat, write out your hash table as well. When you read in the binary tree, read in your hash table as well. (Note that nothing prevents you from putting two different kinds of objects on the same file.)

  2. When you read in the binary tree, traverse it, and put all the animals you find into a new hash table.

Of these two solutions, I think the second is probably easier (and more efficient as well), but it's up to you.