Java Reference
In-Depth Information
24
, because universal-time format represents hours as integers from 0 to 23 (e.g., 1 PM is
hour 13 and 11 PM is hour 23; midnight is hour 0 and noon is hour 12). Similarly, both
minute
and
second
values must be greater than or equal to
0
and less than
60
. For values
outside these ranges,
setTime
throws an exception
of type
IllegalArgumentException
(lines 18-19), which notifies the client code that an invalid argument was passed to the
method. As you learned in Section 7.5, you can use
try
...
catch
to catch exceptions and
attempt to recover from them, which we'll do in Fig. 8.2. The class instance creation ex-
pression in the
throw
statement
(Fig. 8.1; line 18) creates a new object of type
Illegal-
ArgumentException
. The parentheses following the class name indicate a call to the
IllegalArgumentException
constructor. In this case, we call the constructor that allows
us to specify a custom error message. After the exception object is created, the
throw
state-
ment immediately terminates method
setTime
and the exception is returned to the calling
method that attempted to set the time. If the argument values are all valid, lines 22-24
assign them to the
hour
,
minute
and
second
instance variables.
Software Engineering Observation 8.1
For a method like
setTime
in Fig. 8.1, validate all of the method's arguments before using
them to set instance variable values to ensure that the object's data is modified only if all
the arguments are valid.
Method
toUniversalString
Method
toUniversalString
(lines 28-31) takes no arguments and returns a
String
in
universal-time format
, consisting of two digits each for the hour, minute and second—re-
call that you can use the
0
flag in a
printf
format specification (e.g.,
"%02d"
) to display
leading zeros for a value that doesn't use all the character positions in the specified field
width. For example, if the time were 1:30:07 PM, the method would return
13:30:07
.
Line 30 uses
static
method
format
of class
String
to return a
String
containing the for-
matted
hour
,
minute
and
second
values, each with two digits and possibly a leading
0
(specified with the
0
flag). Method
format
is similar to method
System.out.printf
ex-
cept that
format
returns
a formatted
String
rather than displaying it in a command win-
dow. The formatted
String
is returned by method
toUniversalString
.
Method
toString
Method
toString
(lines 34-39) takes no arguments and returns a
String
in
standard-time
format
, consisting of the
hour
,
minute
and
second
values separated by colons and followed
by AM or PM (e.g.,
11:30:17
AM
or
1:27:06
PM
). Like method
toUniversalString
, meth-
od
toString
uses
static
String
method
format
to format the
minute
and
second
as two-
digit values, with leading zeros if necessary. Line 37 uses a conditional operator (
?:
) to de-
termine the value for
hour
in the
String
—if the
hour
is
0
or
12
(AM or PM), it appears
as 12; otherwise, it appears as a value from 1 to 11. The conditional operator in line 30
determines whether AM or PM will be returned as part of the
String
.
Recall all objects in Java have a
toString
method that returns a
String
representation
of the object. We chose to return a
String
containing the time in standard-time format.
Method
toString
is called
implicitly
whenever a
Time1
object appears in the code where
a
String
is needed, such as the value to output with a
%s
format specifier in a call to
System.out.printf
. You may also call
toString
explicitly
to obtain a
String
representa-
tion of a
Time
object.