Java Reference
In-Depth Information
Exercise 1.3 Try invoking the moveVertical , slowMoveVertical , and changeSize
methods before you read on. Find out how you can use moveHorizontal to move the circle
70 pixels to the left.
The additional values that some methods require are called parameters. A method indicates
what kinds of parameters it requires. When calling, for example, the moveHorizontal method
as shown in Figure 1.4, the dialog displays the line near the top.
The header of a
method is called its
signature . It pro-
vides information
needed to invoke
that method.
void moveHorizontal(int distance)
This is called the signature of the method. The signature provides some information about the
method in question. The part enclosed by parentheses ( int distance ) is the information
about the required parameter. For each parameter, it defines a type and a name. The signature
above states that the method requires one parameter of type int named distance . The name
gives a hint about the meaning of the data expected.
Data types
A type specifies what kind of data can be passed to a parameter. The type int signifies
whole numbers (also called “integer” numbers, hence the abbreviation “int”).
In the example above, the signature of the moveHorizontal method states that, before the
method can execute, we need to supply a whole number specifying the distance to move. The
data entry field shown in Figure 1.5 then lets you enter that number.
Parameters have
types . The type
defines what kinds
of values a param-
eter can take.
In the examples so far, the only data type we have seen has been int . The parameters of the
move methods and the changeSize method are all of that type.
Closer inspection of the object's pop-up menu shows that the method entries in the menu in-
clude the parameter types. If a method has no parameter, the method name is followed by an
empty set of parentheses. If it has a parameter, the type and name of that parameter is displayed.
In the list of methods for a circle, you will see one method with a different parameter type: the
changeColor method has a parameter of type String .
The String type indicates that a section of text (for example, a word or a sentence) is expected.
Strings are always enclosed within double quotes. For example, to enter the word red as a string, type
The method-call dialog also includes a section of text called a comment above the method signature.
Comments are included to provide information to the (human) reader and are described in Chapter 2.
The comment of the changeColor method describes what color names the system knows about.
Exercise 1.4 Invoke the changeColor method on one of your circle objects and enter the
string "red" . This should change the color of the circle. Try other colors.
Exercise 1.5 This is a very simple example, and not many colors are supported. See what
happens when you specify a color that is not known.
Search WWH ::

Custom Search