Java Reference
In-Depth Information
6
private int hour; // 0 - 23
7
private int minute; // 0 - 59
8
private int second; // 0 - 59
9
10
// Time2 no-argument constructor:
11
// initializes each instance variable to zero
12
public Time2()
13
{
14
this ( 0 , 0 , 0 ); // invoke constructor with three arguments
15
}
16
17
// Time2 constructor: hour supplied, minute and second defaulted to 0
18
public Time2( int hour)
19
{
20
this (hour, 0 , 0 ); // invoke constructor with three arguments
21
}
22
23
// Time2 constructor: hour and minute supplied, second defaulted to 0
24
public Time2( int hour, int minute)
25
{
26
this (hour, minute, 0 ); // invoke constructor with three arguments
27
}
28
29
// Time2 constructor: hour, minute and second supplied
30
public Time2( int hour, int minute, int second)
31
{
32
if (hour < 0 || hour >= 24 )
33
throw new IllegalArgumentException( "hour must be 0-23" );
34
35
if (minute < 0 || minute >= 60 )
36
throw new IllegalArgumentException( "minute must be 0-59" );
37
38
if (second < 0 || second >= 60 )
39
throw new IllegalArgumentException( "second must be 0-59" );
40
41
this .hour = hour;
42
this .minute = minute;
43
this .second = second;
44
}
45
46
// Time2 constructor: another Time2 object supplied
47
public Time2(Time2 time)
48
{
49
// invoke constructor with three arguments
this (time.getHour(), time.getMinute(), time.getSecond());
50
51
}
52
53
// Set Methods
54
// set a new time value using universal time;
55
// validate the data
56
public void setTime( int hour, int minute, int second)
57
{
Fig. 8.5 | Time2 class with overloaded constructors. (Part 2 of 4.)
Search WWH ::




Custom Search