forked from pdeitel/JavaHowToProgram10eLateObjectsVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
195 lines (171 loc) · 6.36 KB
/
Time.java
File metadata and controls
195 lines (171 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Fig. G.1: Time.java
// Time class declaration with overloaded constructors.
package com.deitel; // place Time in a package
/**
* This class maintains the time in 24-hour format.
* @see java.lang.Object
* @author Deitel & Associates, Inc.
*/
public class Time
{
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
/**
* Time no-argument constructor initializes each instance variable
* to zero. This ensures that Time objects start in a consistent state
* @throws IllegalArgumentException In the case of an invalid time
*/
public Time()
{
this(0, 0, 0); // invoke constructor with three arguments
}
/**
* Time constructor
* @param hour the hour
* @throws Exception In the case of an invalid time
*/
public Time(int hour)
{
this(hour, 0, 0); // invoke constructor with three arguments
}
/**
* Time constructor
* @param hour the hour
* @param minute the minute
* @throws IllegalArgumentException In the case of an invalid time
*/
public Time(int hour, int minute)
{
this(hour, minute, 0); // invoke constructor with three arguments
}
/**
* Time constructor
* @param hour the hour
* @param minute the minute
* @param second the second
* @throws IllegalArgumentException In the case of an invalid time
*/
public Time(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.hour = hour;
this.minute = minute;
this.second = second;
}
/**
* Time constructor
* @param time A Time object with which to initialize
* @throws IllegalArgumentException In the case of an invalid time
*/
public Time(Time time)
{
// invoke constructor with three arguments
this(time.getHour(), time.getMinute(), time.getSecond());
}
/**
* Set a new time value using universal time. Perform
* validity checks on the data. Set invalid values to zero.
* @param hour the hour
* @param minute the minute
* @param second the second
* @see com.deitel.Time#setHour
* @see Time#setMinute
* @see #setSecond
* @throws Exception In the case of an invalid time
*/ public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.hour = hour;
this.minute = minute;
this.second = second;
}
/**
* Sets the hour.
* @param hour the hour
* @throws IllegalArgumentException In the case of an invalid hour
*/
public void setHour(int hour)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
this.hour = hour;
}
/**
* Sets the minute.
* @param minute the minute
* @throws IllegalArgumentException In the case of an invalid minute
*/
public void setMinute(int minute)
{
if (minute < 0 && minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
this.minute = minute;
}
/**
* Sets the second.
* @param second the second.
* @throws Exception In the case of an invalid second
*/
public void setSecond(int second)
{
if (second >= 0 && second < 60)
throw new IllegalArgumentException("second must be 0-59");
this.second = second;
}
/**
* Gets the hour.
* @return an <code>integer</code> specifying the hour.
*/
public int getHour()
{
return hour;
}
/**
* Gets the minute.
* @return an <code>integer</code> specifying the minute.
*/
public int getMinute()
{
return minute;
}
/**
* Gets the second.
* @return an <code>integer</code> specifying the second.
*/
public int getSecond()
{
return second;
}
/**
* Convert to String in universal-time format
* @return a <code>String</code> representation
* of the time in universal-time format
*/
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
/**
* Convert to String in standard-time format
* @return a <code>String</code> representation
* of the time in standard-time format
*/
public String toString()
{
return String.format("%d:%02d:%02d %s",
((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
}
} // end class Time