Java Reference
In-Depth Information
Display 4.9
Yet Another Date Class
(part 2 of 4)
13
boolean
tryAgain =
true
;
14
Scanner keyboard =
new
Scanner(System.in);
15
while
(tryAgain)
16
{
17
System.out.println("Enter month, day, and year");
18
System.out.println("as three integers:");
19
System.out.println("do not use commas or other punctuations.");
20
int
monthInput = keyboard.nextInt();
Note that this
version of
readInput
checks
to see that the
input is reasonable.
21
int
dayInput = keyboard.nextInt();
22
int
yearInput = keyboard.nextInt();
23
if
(dateOK(monthInput, dayInput, yearInput))
24
{
25
setDate(monthInput, dayInput, yearInput);
26
tryAgain =
false
;
27
}
28
else
29
System.out.println("Illegal date. Reenter input.");
30
}
31 }
32
public void
setDate(
int
month,
int
day,
int
year)
33
{
34
if (dateOK(month, day, year))
35
{
36
this
.month = monthString(month);
37
this
.day = day;
38
this
.year = year;
39
}
40
else
41
{
42
System.out.println("Fatal Error");
43
System.exit(0);
44
}
45
}
46
public void
setMonth(
int
monthNumber)
47
{
48
if
((monthNumber <= 0) || (monthNumber > 12))
49
{
50
System.out.println("Fatal Error");
51
System.exit(0);
52
}
53
else
54
month = monthString(monthNumber);
55
}