![]() ![]() int getLeavesEaten() void setDirection(int direction) The parentheses after the method name hold the parameter list. When the wombat eats a leaf the int (integer) will increase to 1.ġ5 Parameters The other bit in the method menu that we have not yet discussed are the parentheses after the method name. This means the wombat can move forward.ġ3 This example shows the canMove() method call returning false since the wombat cannot move since it is at the edge of the game world.ġ4 Here we have the getLeavesEaten() method returning 0 since the wombat has not eaten any leaves. In this case the methods is telling us that the wombat can move if we want it to move.ġ2 Here we see the Greenfoot interface with the method canMove() returning true. This example shows a return type of true, which is a boolean return type. The return type of a method specifies what a method call will return. ![]() They just carry out their action, and then stop.ġ1 The return type of a method specifies what a method call will return The word void means “nothing” in this context: Methods with a void return type do not return any information. It tells us what the method returns to us when we invoke it. Right Click on the Wombat Invoke the Move methodġ0 Return Types The word at the beginning is called the return type. ![]() Interact with objects through the contextual menu by right clicking on the object to see what methods can be called. ![]() The game world for the wombat is part of the world class.Ĩ Make three wombats and three leaves as shown here. A wombat is an object of the Actor class. We can invoke a method by selecting it from the menu.ħ Objects and Classes Right Click on Wombat Drag to World Click New Wombat() This is how to make a wombat appear in the game world. It cannot hurt to get used to standard terminology straightaway, so we shall also call them methods from now on. In Java, these operations are called methods. For example, a wombat’s object menu shows us what this wombat can do (plus two additional functions, Inspect and Remove, which we shall discuss later). The object menu shows us all the operations this specific object can perform. This will pop up the object menu (Figure 1.4). (Objects are also often referred to as instances in programming-the two terms are synonyms.)Ħ Objects and Classes Once we have placed some objects into the world, we can interact with these objects by rightclicking them. Once we have a class in Greenfoot, we can create objects. The class Wombat stands for the general concept of a wombat-it describes all wombats. Let us start by looking at the Wombat class. The concepts of classes and objects are fundamental in object orientation. We shall be using the Java programming language for our projects. In this case, they are World, WombatWorld, Actor, Leaf, and Wombat. The class diagram shows us the classes involved in this scenario. We’ll come back to them in a little while, too.ĥ Objects and Classes We shall discuss the class diagram first. The Act, Run, and Reset buttons and the speed slider at the bottom are the execution controls. We shall discuss this in more detail shortly. The area on the right with the beige-colored boxes and arrows is the class diagram. This is where the program will run and we will see things happen. The largest area covering most of the screen (a sand-colored grid in this case) is called the world. ![]()
0 Comments
Leave a Reply. |