

What is special about a method with a void return type? World, Class diagram, compile button, a class, an object Now, use the new method goAroundTree() inside the act() method for each of the three trees.įind the following elements in the image: Below the act() method we will create a new method:īetween the curly braces of the method, write the commands to make Kara go around a tree. We can expand our program for clarity by introducing a new method. If you have solved task 1.07 correctly, your program should contain three equal parts, namely for walking around each tree. Kara gets new Methods TASK 1.08: Around Tree with Method Arriving at the leaf, Kara should pick it up. Write a program that takes Kara to the leaf along the dotted line on the image above. Public static final String WORLD_SETUP_FILE = ".txt" In order for your world to actually be loaded you must change the constant WORLD_SETUP_FILE in KaraWorld: Tip: So that you don’t have to recreate the world after each compile you can save the world by right-clicking on the World and calling saveWorldSetupToFile(). Note: After each command we must include a semicolon! TASK 1.07: Around Tree (At the beginning of the class you will find a comment describing all the actions that Kara can per-form.) The Translation Process (Compiling)Ĭhange the content of the act() method so that Kara makes a step first, then puts a leaf down and takes a step again. Once the classes are translated, the stripes disappear and we can create objects again. The classes in Greenfoot are translated using the button Compile (bottom right of the main window).
GREENFOOT PIANO TUTORIAL CODE
Compiling is a process of translation: the source code of the class is translated into machine code that can run on the computer. This is an indication that the class has been modified and now needs to be compiled. You will notice that the MyKara class is shown with gray stripes. When you have finished making changes, close the editor window. You can now modify and extend these commands. Here, the three commands: move(), turnRight() and move() are called. For the moment we are only interested in the lowest part where the method act() is defined. The source code is written in Java and contains all the details about a class and its objects. For this we need to open the source code in the editor: Right-click on the class MyKara then choose Open editor (or just use a double-click). Now let’s see where the behavior of this method is programmed.

This is the area when the program runs, we can see what happens.

The world: The largest area is called world.The Greenfoot window consists of three main areas and a few additional buttons. It now appears in the main Greenfoot window similar to the following: Open the scenario Kara 101 First Steps from the folder scenarios-chapter-1. (If you open Greenfoot for the first time, a dialog box asks what you’d like to do. In this chapter we will take our first steps in programming.
