Java Reference
In-Depth Information
Display 5.20
The Class
Date
(Partial Definition)
(part 2 of 2)
39
System.out.println("Fatal Error");
40
System.exit(0);
41
}
42
else
43
this
.year = year;
44
}
45
46
The complete definition of
equals
is
given in the answer to Self-Test
Exercise 37, and is a better version
than the one given in Chapter 4.
public
String toString()
47
...
48
public boolean
equals(Date otherDate)
49
....
/**
50
Returns true if the calling object date is before otherDate (in time).
51
*/
52
public boolean
precedes(Date otherDate)
53
...
54
private
boolean
dateOK(String monthString,
int
dayInt,
int
yearInt)
55
...
These methods have the obvious meanings. If you need to see a full definition,
see Display 4.11 in Chapter 4 and Self-Test Exercise 37 in this chapter.
56
}
Self-Test Exercises
36. What is the difference between the following two pieces of code? The first piece
appears in Display 5.21.
Person adams =
new
Person("John Adams",
new
Date("February", 15, 1947),
null
);
//Second piece is below:
Date theDate =
new
Date("February", 15, 1947);
Person adams =
new
Person("John Adams", theDate,
null
);
37. When we defined the class
Date
in Chapter 4 (Display 4.11), we had not
yet discussed
null
. So, the definition of
equals
given there did not account
for the possibility that the argument could be
null
. Rewrite the definition
of
equals
for the class
Date
to account for the possibility that the argument
might be
null
.