In other words, it expects no parameters. If I tell you to jump, you would ask, “how high?” If the parentheses have nothing in between, e.g. The parentheses hold the parameter list, which tells us whether the method needs any information to run, and if so, what kind of info. IntgetLeavesEaten() Hint if you are stuck: Can you make the wombat eat some leaves?ġ.5 Parameters The other bit in the object menu are the round brackets after the method name. Can you create a situation in which the method returns some positive number? Try out another method with a return value: Using this method, we can find out how many leaves this wombat has eaten. In effect, we just asked the wombat, “Can you move?” And the wombat answers, “Yes!” (true)Įxercise 1.4: Create a new wombat and invoke the getLeavesEaten method. Here, the important information (data) is the word “true” returned by the method call. When you invoke it, you will see a result similar to the one below. What does it usually return? Can you make the method return false? Consider the canMove method. Methods with any other return type are like questions: do what you have to do, but give us an answer when you’re done.Įxercise 1.3: Invoke the canMove() method on any of your wombats. If we invoke turnLeft(), the wombat turns left. In the wombat’s menu we also see the return types: int– short for “integers” (what are some examples?) boolean– one only two possible values will be returned: “true” or “false” Methods with return type void are like commands for our wombats. They just carry out their actions, and then stop.Īny word other than void tells us the type of information returned by the method. The word void means “nothing” in this context: methods of void return type do not return any information. It tells us what the method returns to us when we invoke it. void move() void turnLeft() The word at the beginning is called the return type. These are symbols that tell us what data goes into the method and what comes back from it. There is the word void at the beginning, and round brackets at the end. The move and turnLeftmethods are listed as: We see more than just the method names. Right Click on the Wombat Invoke the Move methodġ.4 Return types Let’s look closer at the object menu. (Notice that they all end with parentheses.) We can invoke a method by clicking on it in the menu. In Java, these operations an object can perform are called methods. Finally, place two wombats into the world and make them face each other. Two other functions, Inspectand Remove, will be discussed later.Įxercise 1.2: Invoke the move() method. In this case, we see what the wombat can do. Right Click on the Wombat Invoke the Move method The object menu shows all the operations the object can perform (how the object can act). ![]() What happens? Extra Credit: Make a journal entry in your notebook about worlds, classes, or objects.ġ.3 Interacting with Objects Once we have placed some objects into the world, we can interact with these objects by right-clicking on them. ![]() Make sure the leaf class is selected, then hold down the Shift key and click into the world. There’s a shortcut to placing objects: shift-clicking into the world. Once you have a class, you can create as many objects from it as you like.Įxercise 1.1: Create some wombats in the world.Make some leaves too. Now place it in the World by clicking somewhere in the world. Try it out! Right Click on Wombat Drag to World Click New Wombat() You’ll get a small picture of a wombat object, which you can move around the screen with your mouse. Here’s how you create objects from the Wombat class. Once we have a class in Greenfoot (or Java), we can create objects from it. It stands for the general concept of a wombat. Let’s start by looking at the Wombat class. They may be different or not exist at all in other scenarios. The classes are specific to the wombat scenario. The classes World and Actor are will always be there – they come with the Greenfoot system. Here, we have 5 classes: World, Wombat World, Actor, Wombat, and Leaf. It shows us the classes involved in this scenario. World Class Diagram Execution Controlsġ.2 Objects and Classes We’ll discuss the Class Diagram first. World Class Diagram Execution ControlsĮxecution Controls: includes the Act, Run, and Reset buttons, and the speed slider. World Class Diagram Execution ControlsĬlass Diagram: This is the little beige boxes and arrows on the right. The World: where programs will run, where we will see things happen. Which leads to the question:“What are wombats?” Wombats are native Australian mammals. ![]() Lee\ Scenarios\ Copy “wombats” folder to “My Documents”. Start Menu | All Programs Greenfoot | Greenfootġ.1 Getting Started Q:\ Assignments\ Mr. Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr.
0 Comments
Leave a Reply. |