CS 1401 Assignment #7
Date Assigned: Tuesday, October 18, 2005 or
Wednesday, October 19, 2005.
Due Date: correspondingly, Tuesday, October 25, 2005, or
Wednesday, October 26, 2005, before the beginning of
your lab section.
Goals: to practice with classes, overloading methods and handling dates.
Points:
This assignment is worth a total of 20 points. No late assignments
will be accepted, but credit will be given for partially completed
assignments turned in on time.
Assignment:
A very prominent use of computers is to maintain databases.
For example, UTEP maintains all student records on databases on computers.
It is a huge task to design a complete database including functionality,
user interface, and considering all the security aspects.
Your assignment is to
design an application which implements simple student records.
The steps and the desired structure are described as follows:
Step 1. Create an object-template StudentRecord class. Include the following
components in the StudentRecord class:
- Define a field for each StudentRecord object to store its student ID number.
The student ID is now composed of digits, but exceptionally, you may have letters
or special symbols, so use the String format.
- Define two fields for each StudentRecord object to store the student's first
and last name.
- Define another field for the student's address.
- Define a field for each studentRecord object to store the last date
it was modified. This field
is of class GregorianCalendar, a class defined in java.util.
Use import java.util.*.
When a GregorianCalendar is created with new without parameters,
it is set to the current date. There is also the constructor of the form
GregorianCalendar(int year, int month, int dayOfMonth).
Months are coded from 0 to 11 instead of the usual 1 to 12.
- Define a constructor method with one parameter, the student ID number.
This constructor should instantiate a student record with the string "unknown" for
name and address and the current date for last date modified.
- Define accessor type methods (a get method to retrieve
the value in a data field) to return the value of the individual StudentRecord fields
(ID number, first and last name, address and last date modified).
- Define mutator type methods (a set method to change the
value in a data field) with a value passed as an argument in order
to set the value of the StudentRecord fields. In each case, except
when setting the last date modified directly, reset the
last date modified to the current date.
- Define a method named toString which returns a string with
the value of all the StudentRecord fields in a form suitable for
output to the screen.
This should include the student ID number as well as the value of
the other fields. For an object cal of type GregorianCalendar, accessing the
year is done by cal.get(Calendar.YEAR), which returns an int. Accessing the
month and date is done with the get method, using MONTH or DAY_OF_MONTH instead of
YEAR.
Again, be careful: the month returns an int which starts at 0 for January. You need to add 1
before converting to a String.
Step 2. Create an application class, including the following tasks:
- Instantiate one StudentRecord object using the default constructor.
Supply your own student ID number.
- Call the accessor method of the StudentRecord and print to the screen
the value of the student ID number field.
- Prompt the user to enter a first and last name and an address.
- Call the mutator methods of the StudentRecord and pass the
user-supplied information in order to reset appropriate fields to the provided
values.
- Call the accessor method of the StudentRecord again and print to the
screen the updated values for the modified fields.
- Use the toString method of the StudentRecord object to print out the
state (field values) of the StudentRecord object, to verify the correct
settings.
- Check to make sure the tasks above are working correctly
before going on to Step 3.
Step 3. Add the following overloaded methods and a helper method to the
object-template class. A method overloads the original method if it has
the same name but different types of parameters. Overloading allows
different versions of method to be used for different situations.
Step 4. Add the following tasks to the application class:
- Call the mutator method defined in Step 3 (the version without
parameters) to reset the address field of the first StudentRecord object back to
the default value, the string "unknown".
- Call the accessor method of the first StudentRecord again and print to
the screen the changed value of the time field to verify that it has
been reset to its default.
- Prompt the user to enter a Student ID number, last and first name,
address and date (ask for date, month and year)
for a second StudentRecord object and read it in.
- Instantiate a second StudentRecord object using the overloaded
constructor defined in Step 3 with the user-supplied information. You will
need to create an object of type GregorianCalendar and set the year, month
and date using the "set" method instead of "get", with the same parameters
(Calendar.YEAR, Calendar.MONTH or Calendar.DAY_OF_MONTH)
- Call the accessor method of the second StudentRecord and print to the
screen the value of the date last modified to verify that the field
has been set to the user provided date.
- Call the synchronization method of the first StudentRecord object to
potentially update the fields of the outdated student record. When the user
provides the same student ID number but a different date, the older record
should be updated.
- Use the toString methods of both StudentRecord objects to print out
the state of both StudentRecord objects.
For extra credit:
Java provides an easy way to save and retrieve
objects. Objects created in an application may be written to a file to
save their current state. Afterwards, the data values may be read back
in to recreate the object. This process is called serialization
(translation from object to bytes in a file) and deserialization (the
reverse operation). To support serialization, the object's class name
must have the words "implements Serializable" following the class name
to indicate that the extra functionality of the serializable interface
is available for the class. The file objects used for output and input
of objects are of type ObjectOutputStream and ObjectInputStream, and the
associated methods are writeObject and readObject. Implement the
serialization interface for the StudentRecord class, and save the current
state of one of your StudentRecord objects to an output file.
Read the object back in using deserialization.
Refer to pp. 354-358 of the textbook for more details.
Please note that you will only be given credit for the extra
credit assignment if your main assignment works.
Deliverables: as announced in the labs.