Assignment #4: 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, Assign4a, 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 Assign4a, twice, sending the result first to
out4a.ser and then to out4b.ser.
- Write,
in a separate file, a program, Assign4b, 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 Assign4b once, using both out4a.ser
and out4b.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 Assign4b on either out4a.ser
or out4b.ser you should get a runtime exception.
- If you run Assign4a and regenerate out4b.ser
you will be able to read it back with Assign5b, but since
out4a.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 Assign4a to regenerate out4b.ser
using the new Person format, BUT LEAVE
out4a.ser IN THE OLD FORMAT, you will
still be able to run Assign4b 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).
What to Submit
Submit your source code,
with the new version of Person
and output that illustrates that you have done this correctly.