1.4 Calling a Method
Calling a Method in Greenfoot
Unit 1: Meet Greenfoot · Lesson 1.4 · about 30 minutes · no coding experience needed
So far you have made things. Now you make them do something. A method is an action an object already knows how to perform, and calling one is how you ask.
What you will be able to do
- Name the three parts of a method call
- Predict what move, turn and setLocation do to an actor
- Call methods on an object directly from the Greenfoot world
Words you will need
- Method
- In plain words An action an object knows how to do.
- More precisely A named block of code belonging to a class, which can be called on any object of that class.
- Call
- In plain words Asking the object to do the action, right now.
- More precisely Writing the method name with parentheses causes the method body to execute.
- Argument
- In plain words The extra information the action needs. The 4 in move(4).
- More precisely A value passed into a method call, supplied inside the parentheses.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen Right-click menu on a crab object in the world, showing its inherited Actor methods including move and turn.
Right-click an OBJECT in the world, not the class in the diagram, and Greenfoot lists everything that object knows how to do. This menu is the best beginner tool in the whole program: you can try any method and watch the result immediately, before you have written a line of code.
-
Step 2
On your screen A dialog asking for the distance argument for the move method, with 4 typed in.
move(4);Choose move and Greenfoot asks you for a number. Type 4. The crab moves four cells in the direction it is FACING. That last part is the bit beginners miss. move(4) does not mean "go to 4" and it does not mean "go right". It means "go four, that way".
Three parts to read: `move` is the name, `(4)` is the argument, `;` ends the statement.
-
Step 3
On your screen The crab rotated 90 degrees after calling the turn method.
turn(90);turn(90) rotates the crab a quarter circle clockwise. Call move(4) again now and it travels in the NEW direction. move and turn work together: turn decides which way, move decides how far.
-
Step 4
On your screen The crab placed at an exact grid position after calling setLocation.
setLocation(0, 0);setLocation is different. It ignores which way the crab is facing and puts it at exact coordinates. setLocation(0, 0) is the TOP LEFT corner, not the middle and not the bottom. In Greenfoot, x grows to the right and y grows DOWNWARD, which surprises everyone who remembers graphs from math.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea move(4) moves the actor to position 4.
What is actually true It moves 4 cells forward from wherever it is, in the direction it faces.
Why it matters Getting this wrong makes every movement bug impossible to reason about.
Common wrong idea y gets bigger as you go up, like in math.
What is actually true In Greenfoot, y gets bigger going DOWN. y = 0 is the top edge.
Why it matters This causes the classic "my jump goes the wrong way" bug in Unit 2.
Common wrong idea You can call a method without parentheses if it needs no information.
What is actually true The parentheses are always required. `getX` is a mention; `getX()` is a call.
Why it matters Missing parentheses is one of the most common first compiler errors.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. A crab is facing right at position (5, 5). You call move(3). Where is it now?
-
2. Which part of `turn(90);` is the argument?
-
3. In Greenfoot, where is the point (0, 0)?
-
4. You want an actor to appear at exactly (10, 2) no matter which way it is facing. Which method?
-
5. Why does `move(4)` need parentheses?
Fill in the code
This code should move a crab forward 5 cells, turn it a quarter turn clockwise, and then move it forward 5 more. Fill in the missing pieces.
move();
(90);
move(5)
Stuck? Open a hint for each blank
- Blank 1: Re-read the goal above the code. It names the distance.
- Blank 2: Which method rotates an actor?
- Blank 3: Every Java statement ends with the same single character.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. An actor faces right. You call turn(180) then move(2). Which way did it travel?
-
2. Which statement moves an actor to the top left corner?
-
3. What is wrong with `move 4;`?
-
4. An actor is at (4, 4) facing down. What does move(2) do?
The short version
- A method call is a name, parentheses, and usually an argument, ending in a semicolon.
- move(n) goes n cells in the direction the actor faces.
- turn(d) rotates by d degrees clockwise.
- setLocation(x, y) places by exact coordinates and ignores facing.
- y grows DOWNWARD. (0, 0) is the top left corner.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]