Chapter 1 - Getting to know Greenfoot

advertisement

Chapter 1 - Getting to know

Greenfoot

Bruce Chittenden

And modified by Mr. Lee

Start Menu | All Programs

Greenfoot | Greenfoot

1.1 Getting Started

Q:\

Assignments\

Mr. Lee\

Scenarios\

Copy “wombats” folder to “My

Documents”.

Open the scenario in Greenfoot.

Which leads to the question:

“What are wombats?”

Wombats are native Australian mammals.

The World: where programs will run, where we will see things happen.

World

Class

Diagram

Execution

Controls

Class Diagram: This is the little beige boxes and arrows on the right. We’ll come back to them later.

World

Class

Diagram

Execution

Controls

Execution Controls: includes the Act, Run, and Reset buttons, and the speed slider. More on them later.

World

Class

Diagram

Execution

Controls

1.2 Objects and Classes

We’ll discuss the Class Diagram first. It shows us the classes involved in this scenario. Here, we have 5 classes:

World, Wombat World, Actor, Wombat, and Leaf.

The classes World and Actor are will always be there – they come with the Greenfoot system.

The classes are specific to the wombat scenario. They may be different or not exist at all in other scenarios.

Let’s start by looking at the Wombat class. It stands for the general concept of a wombat. So this class describes all wombats.

Once we have a class in Greenfoot (or Java), we can create objects from it.

Here’s how you create objects from the Wombat class. 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.

Now place it in the World by clicking somewhere in the world.

Once you have a class, you can create as many objects from it as you like.

Exercise 1.1: Create some wombats in the world.

Make some leaves too.

There’s a shortcut to placing objects: shift-clicking into the world.

Make sure the leaf class is selected, then hold down the Shift key and click into the world. What happens?

Extra Credit: Make a journal entry in your notebook about worlds, classes, or objects.

1.3 Interacting with Objects

Once we have placed some objects into the world, we can interact with these objects by right-clicking on them. The object menu will pop up.

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). In this case, we see what the wombat can do. Two other functions, Inspect and Remove , will be discussed later.

In Java, these operations an object can perform are called methods . (Notice that they all end with parentheses.) We can invoke a method by clicking on it in the menu.

Right Click on the Wombat

Invoke the Move method

Exercise 1.2: Invoke the move() method. What does it do? Try it a few times. Invoke the turnLeft() method. Finally, place two wombats into the world and make them face each other.

1.4 Return types

Let’s look closer at the object menu. The

move and turnLeft methods are listed as: void move() void turnLeft()

We see more than just the method names. There is the word void at the beginning, and round brackets at the end. These are symbols that tell us what data goes into the method and what comes back from it.

The word at the beginning is called the return type . It tells us what the method returns to us when we invoke it. The word void means

“nothing” in this context: methods of void return type do not return any information. They just carry out their actions, and then stop.

Any word other than void tells us the type of information returned by the method.

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. If we invoke turnLeft(), the wombat turns left.

Methods with any other return type are like questions: do what you have to do, but give us an answer when you’re done.

Consider the canMove method.

When you invoke it, you will see a result similar to the one below.

Here, the important information (data) is the word “true” returned by the method call. In effect, we just asked the wombat, “Can you move?

And the wombat answers, “Yes!” (true)

Exercise 1.3: Invoke the canMove() method on any of your wombats.

What does it usually return? Can you make the method return false?

Try out another method with a return value:

Int getLeavesEaten()

Using this method, we can find out how many leaves this wombat has eaten.

Exercise 1.4: Create a new wombat and invoke the getLeavesEaten method. It will always return 0. Can you create a situation in which the method returns some positive number?

Hint if you are stuck: Can you make the wombat eat some leaves?

1.5 Parameters

The other bit in the object menu are the round brackets after the method name.

The parentheses hold the parameter list , which tells us whether the method needs any information to run, and if so, what kind of info. If I tell you to jump, you would ask, “how high?”

If the parentheses have nothing in between, e.g. turnLeft(), the

parameter list is empty. In other words, it expects no parameters.

The entire description of each method shown in the object menu, including the return type, method name, and parameter list, is called the method signature .

Let’s try out the setDirection method. We can see it has int direction as its parameter list, which tell us that the method expects one parameter of type int, which specifies a direction.

Let’s invoke the method, and you will see the following dialog box.

The comment near the top of the dialog box gives more details: the direction parameter should be between 0 and 3.

Exercise 1.5: Invoke the setDirection(int direction) method.

Provide a parameter value and see what happens to the wombat. Which number corresponds to what direction? Make a list on paper.

What happens when you type a number greater than 3? How about a number that is a word (three) or not an integer (e.g.

2.3)?

1.6-1.9 Execution Controls

Exercise 1.6: Place a wombat and a good number of leaves into the world. Then, invoke a wombat's act() method several times.

What does this method do? How does it differ from the move method?

Exercise 1.7: Still with a wombat and some leaves in the world, click the Act button in the executive controls near the bottom of the Greenfoot window. What does this do?

Exercise 1.8: Add a few wombats to the world. What is the difference now between clicking the Act button and invoking the act() method on the wombats?

Exercise 1.9 Click the Run button. What does it do?

Exercise 1.6

Exercise 1.6

Wombat Moves toward the Leaves

Exercise 1.6

Wombat Moves to Leaves

Example 1.7

Wombat Eats Leaf

Exercise 1.8

Exercise 1.8

The >Act Execution Control

Affects All the Wombats

Exercise 1.9

Wombat Runs Around the Edge of the World

Exercise 1.9

act() method

– If we’re sitting on a leaf, eat the leaf

– Otherwise, if we can move forward, move forward

– Otherwise, turn left

– Leaves do nothing when called to act.

• The act() method is fundamental to Greenfoot objects. We will see it in every chapter. All

Greenfoot classes/objects have this method.

Execution Controls, recapped

• The Act button is equivalent to invoking the

act() method of every object exactly once.

(Refer to the previous slide.)

• The Run button is equivalent to continuously clicking the Act button. You may have noticed that when clicked, the Run button becomes a

Pause button, which can stop the whole show.

• The slider to the right of the buttons sets the speed.

A Brave New World

• You can erase objects from the world by rightclicking them and choosing Remove .

• But if the world becomes cluttered, and you want to start all over, there’s a shortcut: discard the world and create a new one.

• This is usually done via the Reset button.

• You will get a new, empty world. The old one has been discarded along with all objects in it – you can have only one world at a time.

Invoke a world method

• We have seen objects in the world have methods you can invoke via a pop-up menu. The world itself is also an object, with methods you can invoke.

• Above the world display is a title that shows the name of the world—”WombatWorld” in this case.

Right-click this title to see the world’s menu.

• One method is populate().

• Reset the world, then try it out. Note the empty parameter list.

• The other method is randomLeaves, which places some leaves at random locations in the world.

• Note the words in the parenthesis—called the

parameter: int howMany

• It means you must supply additional information when you invoke the method.

int means a whole number is expected, and howMany suggests that you can specify how many leaves to place in the world.

• Invoke the randomLeaves method. A dialog will pop up asking for a value for the parameter. What would you enter to get the same number of leaves as the picture below?

1.8 Understanding the Class Diagram

At the top of the diagram, you can see two classes called World and

WombatWorld , connected by an arrow.

The World class is in every Greenfoot scenario—it’s built-in. The class under it, WombatWorld in this case, represents the specific world for this scenario.

The arrow shows an is-a relationship: WombatWorld is a World . We can also say that WombatWorld is a subclass of World .

Animal

Mammal

Bonobo

Human

Below the world classes, we see a second group of classes linked by an arrow.

Each class represents its own objects.

Reading from the bottom, we see Leaves and Wombats . Both are Actors in the world.

Again, we have subclass relationships. For example, Leaf is a subclass of Actor . We can also say that Actor is a superclass of Leaf and Wombat .

What is a superclass and what is a subclass in the diagram to the left?

1.11 Summary

• In this chapter, we saw what Greenfoot scenarios can look like and how to interact with them.

• We have seen how to create objects, and how to communicate with these objects by invoking their methods.

• Some methods are commands to objects, while other methods return information about the object.

• Parameters are used to provide additional information to methods, while return values pass information back to the caller.

• Objects were created from their classes.

Concept Summary

Download