Java Reference
In-Depth Information
Display 5.19
A Person Class
(part 2 of 5)
43
public void
set(String newName, Date birthDate, Date deathDate)
<Definition of this method is Self Test Exercise 4.1>
44
public
String toString()
45 {
46 String diedString;
47
if
(died ==
null
)
48 diedString = "";
//Empty string
49
else
50 diedString = died.toString();
This is the
toString
method of the class
Date
.
51
return
(name + ", " + born + "-" + diedString);
52 }
This is equivalent to
born.toString( )
.
53
public boolean
equals(Person otherPerson)
54 {
55
if
(otherPerson ==
null
)
56
return false
;
57
else
58
return
(name.equals(otherPerson.name)
59 && born.equals(otherPerson.born)
60 && datesMatch(died, otherPerson.died) );
61 }
This is the
equals
method for
the class
String
.
This is the
equals
method for the
class
Date
.
62 /**
63
To match, date1 and date2 must either be the same date or must both be null.
64 */
65
private static boolean
datesMatch(Date date1, Date date2)
66 {
67
if
(date1 ==
null
)
68
return
(date2 ==
null
);
69
else if
(date2 ==
null
) //&& date1 != null
70
return false
;
71
else
//both dates are not
null
.
72
return
(date1.equals(date2));
73 }
74 /**
75
Precondition: newDate is a consistent date of birth.
76
Postcondition: Date of birth of the calling object is newDate.
77 */
78
public void
setBirthDate(Date newDate)
79 {
80
if
(consistent(newDate, died))
81 born =
new
Date(newDate);
82
else