Java Reference
In-Depth Information
Display 4.13
A Class with Constructors
(part 4 of 5)
117
public int
getDay()
118
{
119
return
day;
We have omitted the method
writeOutput
because
it would be superfluous, as noted in the subsection
entitled “The Methods
equals
and
toString.
”
120
}
121
public int
getYear()
122
{
123
return
year;
124
}
125
public
String toString()
126
{
127
return
(month + " " + day + ", " + year);
128
}
The method
equals
of the class
String.
129
public boolean
equals(Date otherDate)
130
{
131
return
( (month.equals(otherDate.month))
132
&& (day == otherDate.day) && (year ==
otherDate.year) );
133
}
134
public Boolean
precedes(Date otherDate)
135 {
136
return
( (year < otherDate.year) ||
137 (year == otherDate.year && getMonth() <
otherDate.getMonth()) ||
138 (year == otherDate.year && month.equals(otherDate.month)
139 && day < otherDate.day) );
140
}
141
public void
readInput()
142 {
143
boolean
tryAgain =
true
;
144 Scanner keyboard =
new
Scanner(System.in);
145
while
(tryAgain)
146 {
147 System.out.println("Enter month, day, and year.");
148 System.out.println("Do not use a comma.");
149 String monthInput = keyboard.next();
150
int
dayInput = keyboard.nextInt();
151
int
yearInput = keyboard.nextInt();
152
if
(dateOK(monthInput, dayInput, yearInput) )
153 {
154 setDate(monthInput, dayInput, yearInput);
155 tryAgain =
false
;
156 }