Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


362


“Get Name object”, “Get Address object”, and “Get Phone object” were previously devel-
oped in Chapter 6. The only difference is that we are reading these objects from a file
rather than from the keyboard, so we don’t need prompts.

//**************************************************************
// This class extends the Entry class with a date field.
// Method writeToFile overloads the method in the superclass.
//**************************************************************

packageaddressBook;
importjava.util.*;
importjava.io.*;
public classEntryPlus extendsEntry implementsSerializable
{
privateGregorianCalendar birthday;
publicEntryPlus(Name newName, Address newAddress, Phone phoneNumber,
GregorianCalendar date)
{
super(newName, newAddress, phoneNumber);
birthday = date;
}
// Knowledge method
publicGregorianCalendar knowBirthday()
{
returnbirthday;
}
public voidwriteToFile(ObjectOutputStream outObject) throwsIOException
{
// The EntryPlus object is written to a file of objects
outObject.writeObject(this);
}
}

Get Birthday
Write "Enter the birthday for " + name.full()
Write "Month: "
Read and convert month to int
Write "Day: "
Read and convert day to int
Write "Year: "
Read and convert year to int
Free download pdf