Java Reference
In-Depth Information
variable
u
contains
null
, then attempting to access a component using
u.
com-
ponent-name
gives an error message.
1.3.6
Self-review exercises
This section introduced many new concepts, and it is important that you digest
them at this point because the rest of the text builds on them. One way to gain
understanding is to use an IDE to practice using the new concepts.
SR1.
In your IDE (probably DrJava), create some folders of class
JFrame
, make
them appear on your monitor (by calling method show), and drag the windows
to different positions and give them different sizes. Now, experiment with call-
ing the methods of Fig. 1.6 in these folders.
SR2.
In this exercise, you will study class
Date
, which is in package
java.util
.
Execute these statements in your IDE:
import
java.util.*;
Date d=
new
Date();
Evaluate
d
to see what value it has. You see the date and time at which the new
Date
folder was created. Have your IDE evaluate this function call:
d.getTime()
. The value printed is the number of milliseconds since 1 January
1970, 00:00:00 GMT (Greenwich Mean Time) until the time given by variable
d
. That is a lot of milliseconds!
Class
Date
has methods that allow you to get and set the various parts of a
date and time. Most of these are
deprecated
(literally, lessened in value) because
there are now better (often more complex) ways to achieve their functionality.
The API specification for class
Date
tells you about this. These methods make
great examples, though, so we still use them. We list some of the methods below;
their names should be enough for you to understand what they do.
getYear() getMonth() getDay()
setYear(
int
) setMonth(
int
) setDay(
int
)
getHours() getMinutes() getSeconds()
setHours(
int
) setMinutes(
int
) setSeconds(
int
)
They come in set/get pairs. Often, a property has both a
setter
and a
getter
method. Sometimes a property is read-only, in which case no setter method is
provided.
The argument for
setYear
is a bit peculiar. Before you begin setting any-
thing using
setYear
, first evaluate
d.getYear()
and then read the Java specifi-
cations for
setYear
and
getYear
.
There are at least two ways to find out whether
setHours
and
getHours
use
12-hour (AM/PM) or 24-hour time. What are they?
Search WWH ::
Custom Search