1.6 Make it patrol

Make it patrol

Unit 1: Meet Greenfoot · Lesson 1.6 The act() Method · code exercise, graded

This is the task the Unit 1 lab ends on. act() runs once per frame, and the repeating is Greenfoot calling it, not anything you write.

What to write

  1. Move the crab 2 forward every frame.
  2. When it reaches the edge of the world, turn it 180 degrees so it walks back.

You have not formally met `if` yet, and that is fine. Read it as English: IF the thing in the parentheses is true, do what is in the braces. Unit 2 covers it properly.

Your code

Press Run to watch your code on the grid. Nothing is recorded by Run, so press it as often as you like. Step goes one frame at a time, which is the view that actually explains what act() does.



What it is checked against

These are the examples you can see. There are also 2 hidden cases with different setups, so printing the answers below as text passes nothing.

Example 1: what we set up

World w = new World(12, 6, 32);
Harness h = new Harness();
w.addObject(h, 2, 3);
for (int i = 0; i < 4; i++)
{
h.act();
}
System.out.println(h.getX() + "," + h.getRotation());

What it should print

10,0

Example 2: what we set up

World w = new World(12, 6, 32);
Harness h = new Harness();
w.addObject(h, 2, 3);
for (int i = 0; i < 8; i++)
{
h.act();
}
System.out.println(h.getX() + "," + h.getRotation());

What it should print

5,180

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]