Assignment #9: Serialization
A Person class is declared as follows:
public final class Person
{
public Person( String n, String d )
{
name = n;
birthDate = d; // such as "Jan 5, 2001"
}
public String toString( )
{
return "Person: " + name + " " + birthDate;
}
private String name;
private String birthDate;
}
- Write,
in a separate file, a program, Assign9a, that constructs
five different Person objects,
places them in a List, and then serializes
the result to a file specified by either a command line argument
or user input (your choice).
Of course, you will need to slightly alter Person to do this.
Run Assign9a, twice, sending the result first to
out9a.ser and then to out9b.ser.
- Write,
in a separate file, a program, Assign9b, that reads
files that contains a serialized list of Person
objects and prints the list.
The files are specified by either a command line argument or
user input (your) choice.
Run Assign9b once, using both out9a.ser
and out9b.ser.
-
The Person class changes.
Change the private birthDate field to an object of
type Date, and change the implementation of the
Person constructor accordingly.
The signature of the constructor is not to change, nor is
the name of the field birthDate; thus the
birthDate object is constructed by calling
the one-parameter java.util.Date constructor.
Do not worry that this constructor is deprecated.
(Note: in both versions, an unknown birthdate is represented by null).
Change the implementation of toString to
indicate that this is the revised Person class,
but otherwise make no changes.
-
After recompiling Person,
at this point you should verify the following:
- If you run Assign9b on either out9a.ser
or out9b.ser you should get a runtime exception.
- If you run Assign9a and regenerate out9b.ser
you will be able to read it back with Assign5b, but since
out9a.ser is in the old format, you won't be able to read that file.
The main part of the assignment is to revise the
Person class in such a way, so that if you
rerun Assign9a to regenerate out9b.ser
using the new Person format, BUT LEAVE
out9a.ser IN THE OLD FORMAT, you will
still be able to run Assign9b for both files as was done before,
even though there are two different Person formats.
In implementing this part,
you may rewrite and recompile only class Person.
I expect that you will check the type of the Person
on the stream, and take appropriate arrangements if it
is the old format (String).