Java Reference
In-Depth Information
SR3.
In this exercise, you will study class
Point
and practice reading the API.
Class
Point
is in package
java.awt
, so type this import statement into Java:
import
java.awt.*;
Now open this url in your browser:
http://java.sun.com/api/index.html
In the upper-left frame in the window, click on “
java.awt
”. In the lower-left
frame, scroll down and select “
Point
”. In the right frame you will see a descrip-
tion of class
Point
. Notice that it says that an instance of class
Point
is a point
representing a location in (
x
,
y
) coordinate space, specified in integer precision,
by which they mean type
int
. Scroll down until you see “Field Summary”,
“Constructor Summary”, and “Method Summary”. These show you the compo-
nents of every object of class
Point
. On a piece of paper, draw one folder
(instance) of class
Point
, putting in it all the components. Do not forget to draw
x
and
y
as variables, and for each method, include the types of its parameters.
Create two instances of class
Point
:
Point p1=
new
Point();
Point p2=
new
Point(5, 6);
Class
Point
lets you access the fields
x
and
y
. See what their values are in
p1
and
p2
. For example, evaluate
p2.x
.
In the
Point
webpage, the summary for
x
does not say what its initial value
is. To find out what value
x
has when you do
new
Point()
click on “
Point()
” in the Constructor Summary. See whether the information
that comes up tells you. Then use the back button to get back to the Constructor
Summary.
Now look through the Method summaries. Type in some method calls and
see what happens. For example, try, one after the other, these method calls:
p2.getX()
p2.translate(5, 6);
p2.getX()
See what function
toString
gives you by typing in
p2.toString()
.
Experiment until you are familiar with
Point
and its methods.
1.4
Customizing a class to suit our needs
1.4.1
A subclass definition
Suppose we want to change the title of a
JFrame
to contain its origin (the posi-
Search WWH ::
Custom Search