Java Reference
In-Depth Information
Figure 1.4
A drawing of a circle
Concept:
Exercise 1.2
What happens if you call
moveDown
twice? Or three times? What happens if
you call
makeInvisible
twice?
We can com-
municate with
objects by invoking
methods
on them.
Objects usually do
something if we in-
voke a method.
The entries in the circle's menu represent operations that you can use to manipulate the circle.
These are called
methods
in Java. Using common terminology, we say that these methods are
called
or
invoked.
We shall use this proper terminology from now on. We might ask you to “in-
voke the
moveRight
method of
circle1
.”
1.4
Parameters
Now invoke the
moveHorizontal
method. You will see a dialog appear that prompts you
for some input (Figure 1.5). Type in 50 and click OK. You will see the circle move 50 pixels
to the right.
2
Concept:
The
moveHorizontal
method that was just called is written in such a way that it requires
some more information to execute. In this case, the information required is the distance—how
far the circle should be moved. Thus, the
moveHorizontal
method is more flexible than
the
moveRight
and
moveLeft
methods. The latter always move the circle a fixed distance,
whereas
moveHorizontal
lets you specify how far you want to move the circle.
Methods can have
parameters
to
provide additional
information for a
task.
Figure 1.5
A method-call dialog
2
A pixel is a single dot on your screen. Your whole screen is made up of a grid of single pixels.
Search WWH ::
Custom Search