looks like this:
public void act()
{
// Add your action code here.
}
This bit of code specifies what the
turtle does when it acts. The code
between the curly braces ({ and }) is
executed every time the turtle acts. In
this case, all that is written here is
the following:
you click the Act button, the act method
is executed once, so the turtle moves a
little bit ( 2 pixels) to the right. Clicking
the Run button calls the act method over
and over again (until you pause again),
so the turtle keeps moving.
turn( 2);
Errors
You might have noticed that
you need to write your code
very precisely. Getting even
one character wrong makes
the whole program not work.
The compiler then reports an
error message, and you need
to fix your code.
If you have not yet seen an
error message, try it now. For
example, remove the semicolon after your instruction and
try to compile. You’ll see what I mean.
Figure 7
JAVA IN ACTION
// Add your action code here.
The double slash at the beginning of
the line marks this line as a comment. It
is ignored by the Java system and is just
a reminder for the human programmer.
In other words, this act method contains
no code at all. That’s why our turtle does
not do anything.
Let’s change that.
3. Replace the comment with an instruction so that your act method
looks like this:
Remember: Change the code, compile, create a new turtle, and then run.
Experiment with different values here
as well.
Sequences of Instructions
You can write multiple instructions, as
many as you like, into your act method.
In fact, you can write one after the other:
public void act()
{
move( 2);
}
4. Compile, create a new turtle, and try
clicking the Act button and the Run
button.
So, what did we just do?
The instruction move( 2); tells the
turtle to move two pixels forward. The
turtle does this every time it acts. When
Methods and Parameters
So, what have we just done?
We have called (or invoked) a method
called move and a method called turn,
and we have passed a parameter,
2, to
each of them.
A method is a bit of behavior that
an object knows to execute, and all
actors know the move and turn methods (meaning they know how to move
and turn).
Both of these methods expect a
parameter, which is a bit of additional
information that tells them exactly how
far to move or how much to turn. Both
methods expect a number as a parameter, and we supply that number by writing it in parentheses after the name of
the method. And each instruction in Java
is ended with a semicolon.
public void act()
{
move( 4);
turn( 2);
}
in the object’s class.
ABOUT US
Try this out. Also, place multiple turtles into the world, and experiment with
different parameter values for both the
move and turn methods (see Figure 7).
Summary
In this article, you learned the first few
steps of writing your own Java code,
which demonstrated the following
principles:
■ ■ The behavior of an object is specified
in the object’s class.
■ ■ More precisely, the behavior of an object is specified in a method definition
LEARN MORE
•;Greenfoot
•;Java SE API
•;Young Developer Resources
•;Young Developers Series
“Wombat Object Basics (Part 1)”
“Wombat Classes Basics (Part 2)”
blog