chapter 1 - getting to know greenfoot bruce chittenden and modified by mr. lee

39
Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Upload: jeremy-franklin

Post on 01-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Chapter 1 - Getting to know Greenfoot

Bruce ChittendenAnd modified by Mr. Lee

Page 2: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Start Menu | All Programs

Greenfoot | Greenfoot

Page 3: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

1.1 Getting Started

Q:\ Assignments\ Mr. Lee\ Scenarios\

Copy “wombats”folder to “MyDocuments”.

Open the scenarioin Greenfoot.

Page 4: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Which leads to the question:“What are wombats?”

Wombats are native Australian mammals.

Page 5: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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

World

ClassDiagram

ExecutionControls

Page 6: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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

World

ClassDiagramExecution

Controls

Page 7: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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

World

ClassDiagram

ExecutionControls

Page 8: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

1.2 Objects and ClassesWe’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.

Page 9: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Right Click on Wombat

Click New Wombat()

Drag to World

Here’s how you create objects from the Wombat class. Try it out!

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.

Page 10: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.1: Create some wombats in the world.Make some leaves too.

Page 11: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 12: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

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.

1.3 Interacting with Objects

Invoke the Move method

Right Click on the Wombat

Page 13: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Invoke the Move method

Right Click on the Wombat

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.

Page 14: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Let’s look closer at the object menu. The move and turnLeft methods are listed as:

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.

1.4 Return types

void move()void turnLeft()

Page 15: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 16: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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?

Page 17: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Try out another method with a return value:

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

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

Int getLeavesEaten()

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?

Page 18: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

1.5 Parameters

Page 19: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 20: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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)?

Page 21: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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?

1.6-1.9 Execution Controls

Page 22: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.6

Page 23: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.6

Wombat Moves toward the Leaves

Page 24: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.6

Wombat Moves to Leaves

Page 25: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Example 1.7

Wombat Eats Leaf

Page 26: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.8

Page 27: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.8

The >Act Execution Control Affects All the Wombats

Page 28: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Exercise 1.9

Wombat Runs Around the Edge of the World

Page 29: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 30: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 31: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

A Brave New World• You can erase objects from the world by right-

clicking 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.

Page 32: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 33: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

• One method is populate().• Reset the world, then try it out. Note the empty

parameter list.

Page 34: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

• 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.

Page 35: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

• 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?

Page 36: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

1.8 Understanding the Class DiagramAt 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.

Page 37: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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?

Animal

Mammal

Bonobo

Human

Page 38: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

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.

Page 39: Chapter 1 - Getting to know Greenfoot Bruce Chittenden And modified by Mr. Lee

Concept Summary