In this lab you will experiment with reading and writing objects directly
and as text. DO NOT USE THE KEYBOARD CLASS ANYWHERE!!
Create and test a Student class
- Write a Student class that holds info about a student -- name (String),
age (int), and gpa (double). Provide the following public methods:
- Student() -- a parameterless constructor that
generates a student with a random age in the range 17-25 and a random gpa
in the range 0.0-4.0. Name each student "J. DoeN," where N indicates the
number of students that have already been created. So the first student will
be J. Doe0, the second will be J. Doe1, and so on. You will need to use
a static variable to do this.
- String toString() -- return a string that contains the information about
a student, nicely formatted and labeled, on a single line.
- Write a test program WriteStudents to generate and print 5 random
students.
Run your program several times; you
should get different students each time. Note that you don't need to
store each student after you print it, so you can use a single Student
variable.
Write student objects to a file and read back in
- Modify your WriteStudents program so that after generating each student,
you use an ObjectOutputStream to write it as an object to a file. You will
need to prompt for and read in the name of the file to use.
Recall that to do
this you will need to make the Student class Serializable, and that the
Serializable interface is in the java.io package. Everything else in the
program should remain the same. Remember to close the file when you are done
writing.
After you run your program, check to make sure the file you wrote to exists.
Inspect its contents with less, more, or emacs. Yuk!
- Write a program ReadStudents that prompts for and reads in
a filename, opens an ObjectInputStream to that file, and reads 5 student
objects from that file, printing each after you read it.
- Test your object reading and writing by running WriteStudents, writing
the output to some file, then running ReadStudents, taking the input from
the same file that you just wrote. They should print the same students.
Write variable number of student objects to a file and read back in
- Modify your WriteStudents program so that instead of always creating
and writing 5 students, it randomly generates an integer between 1 and 10
and generates/writes that many students. Thus the number of students will
vary from one run to another.
- Modify your ReadStudents program to deal with varying numbers of students
in the file. Note that attempting to read from an ObjectInputStream that is
at EOF throws an EOFException.
Write students to a text file and read back in
- Now modify your WriteStudents program so that after writing
the students as objects to a file, it writes them as text to a second file.
You will need to prompt for and read in the name of this second file as well.
Think about how to do this: you can add a method to the Student class
that takes a PrintWriter
and writes the data to that stream, or you can add methods to get the data
and write it from the outside. Consider the advantages of each approach,
then choose one.
Either way, think about the format you want to use in writing
the information, as you will have to read it
back in.
After you run your program, check to make sure both output files exists, and
inspect the contents of both.
- Modify your ReadStudents program so that after reading and printing
the students from the object file, it reads and prints them from the
text file. Again, you will need to prompt for both file names. And again
you can choose between providing a method to do the reading and doing the
reading from the outside, then constructing a student with the data.
Test your program. You should get the same output twice, once from the
object file and once from the text file.
Write array of students to a file and read back in
- Modify your WriteStudents program so that you store the students
in an array as you create them. (Keep writing them to the object and text
files as you did before.) After you have filled the array, use a single
statement to write it as an object to a third file. (Yes, you'll have to prompt for
yet a third filename.) Now modify your ReadStudents program to prompt
for this filename along with the others, and to read the entire array from it,
again with one statement. You will still need to use a loop to print the
array elements, but the reading should be outside the loop. Now you should
have three sets of identical output, each clearly labeled.
- Which was easier to do, the object I/O or the text I/O? Discuss
the advantages and disadvantages of each.