SOLUTION TASK 1.02
- What does the move() method do?
Kara moves ahead one field. - Place two Karas in your world and make sure that they face each other. Which method do you need?
turnLeft() or turnRight() - Try the other methods. There are two types of methods. What are those types and what do they do?
Method with void: Perform an action.
Methods with boolean: Open a window containing the methods result.
SOLUTION TASK 1.03
- Right-click on the Kara object and call the
onLeaf()
method. Does it always returnfalse
? If no, in which situations does it returntrue
?
If Kara is on a leaf this method returns true. - Add an additional tree to the world. What method can you call to check if Kara is facing a tree?
treeFront() - What happens if you call Kara’s
move()
method when Kara is facing a tree?
Kara complains that he can’t move.
SOLUTION TASK 1.04
What are the values of the coordinates and rotation in the following situations?
-
x: 0, y: 0, rotation: 0
-
x: 1, y: 2, rotation: 180
SOLUTION TASK 1.05
- Place a
MyKara
object into your world. What new method do you have available?
act() - What does this method do?
Move, turn right, move - What happens if you press the Act button in the Greenfoot controls?
The same as before, the act() Method is called - Click on the Run button. What is happening? Try using the slider.
act() is called again and again until the pause button is clicked - What methods can be found when you right-click on MyKara and then inherited from Kara? Can you use them?
All methods from Kara. They can directly be used by MyKara
SOLUTION TASK 1.06
public class MyKara extends Kara { public void act() { move(); putLeaf(); move(); stop(); } }
SOLUTION TASK 1.07
public class MyKara extends Kara { public void act() { move(); turnLeft(); move(); turnRight(); move(); move(); turnRight(); move(); turnLeft(); turnLeft(); move(); turnRight(); move(); move(); turnRight(); move(); turnLeft(); move(); turnLeft(); move(); turnRight(); move(); move(); turnRight(); move(); turnLeft(); removeLeaf(); stop(); } }
Notes
- The comments in the code have been omitted (= the text that is in the Greenfoot editor either gray or blue). Comments in the source code are written as additional information. The comments are for people and are ignored by the computer. There are three ways to post comments in the code:
- With a double slash
//
(after the double slash the rest of the line is a comment) - Longer comments over several lines are enclosed between
/*
and*/
. - Comments on methods and classes are written between
/**
and*/
. - The method
stop()
makes sure the simulation stops after theact()
-Method even when the Run-button was pressed.
SOLUTION TASK 1.08
public class MyKara extends Kara { public void act() { move(); goAroundTree(); goAroundTree(); move(); goAroundTree(); removeLeaf(); stop(); } public void goAroundTree() { turnLeft(); move(); turnRight(); move(); move(); turnRight(); move(); turnLeft(); } }
Notes
- For greater clarity and to avoid that we need to write the same code three times, we have developed a new method
goAroundTree()
. - The name of the methods here are preceded by two keywords:
public void
.public
means that the method can be called from outside.void
means that the method returns no value. - Behind each method is a pair of parentheses
()
which means that the method gets passed no parameters. Later we will learn how to write methods with parameters.