Java Reference
In-Depth Information
Display 5.19
A Person Class
(part 1 of 5)
1
/**
2
Class for a person with a name and dates for birth and death.
3
Class invariant: A Person always has a date of birth, and if the Person
4
has a date of death, then the date of death is equal to or later than the
5
date of birth.
6
*/
7
public
class
Person
8{
9
The class
Date
was defined in Display 4.11 and
many of the details are repeated in Display
5.20.
private
String name;
10
private
Date born;
11
private
Date died;
//null indicates still alive.
12
public
Person(String initialName, Date birthDate, Date deathDate)
13
{
14
if
(consistent(birthDate, deathDate))
15
{
16
name = initialName;
We will discuss
Date
and
the significance of these
constructor invocations in
the subsection entitled
“Copy Constructors.”
17
born =
new
Date(birthDate);
18
if
(deathDate ==
null
)
19
died =
null
;
20
else
21
died =
new
Date(deathDate);
22
}
23
else
24
{
25
System.out.println("Inconsistent dates. Aborting.");
26
System.exit(0);
27
}
28
}
29
public
Person(Person original)
Copy constructor
30
{
31
if
(original ==
null
)
32
{
33
System.out.println("Fatal error.");
34
System.exit(0);
35
}
36
name = original.name;
37
born =
new
Date(original.born);
38
if
(original.died ==
null
)
39
died =
null
;
40
else
41
died =
new
Date(original.died);
42
}
(continued)