Java Reference
In-Depth Information
Display 5.20
The Class
Date
(Partial Dei nition)
(part 1 of 2)
This is not a complete definition of the class
Date
.
The complete definition of the class
Date
is in Display 4.11,
but this has the details that are important to what we are
discussing in this chapter .
1
public class
Date
2 {
3
private
String month;
4
private int
day;
5
private int
year; //A four digit number.
6
public
Date(String monthString,
int
day,
int
year)
7 {
8
setDate
(monthString, day, year);
9 }
10
public
Date(Date aDate)
11 {
12
if
(aDate ==
null
) //Not a real date.
13 {
14 System.out.println("Fatal Error.");
15 System.exit(0);
16 }
Copy constructor.
17 month = aDate.month;
18 day = aDate.day;
19 year = aDate.year;
20 }
21
public void
setDate(String monthString,
int
day,
int
year)
22 {
23
if
(dateOK(monthString, day, year))
24 {
25
this
.month = monthString;
26
this
.day = day;
27
this
.year = year;
28 }
29
else
30 {
31 System.out.println("Fatal Error");
32 System.exit(0);
33 }
34 }
35
public void
setYear(
int
year)
36 {
37
if
( (year < 1000) || (year > 9999) )
38 {
The method
dateOK
checks that the
date is a legitimate date, such as
not having more than 31 days.