Bruce Chittenden And modified by Mr Lee Start Menu All Programs Greenfoot Greenfoot 11 Getting Started Q Assignments Mr Lee Scenarios Copy wombats folder to My ID: 322532
Download Presentation The PPT/PDF document "Chapter 1 - Getting to know Greenfoot" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Chapter 1 - Getting to know Greenfoot
Bruce Chittenden
And modified by Mr. LeeSlide2
Start Menu | All Programs
Greenfoot | GreenfootSlide3
1.1 Getting Started
Q:\
Assignments\
Mr. Lee\
Scenarios\Copy “wombats”folder to “MyDocuments”.Open the scenarioin Greenfoot.Slide4
Which leads to the question:
“What are wombats?”
Wombats are native Australian mammals.Slide5
The World: where programs will run, where we will see things happen.
World
Class
Diagram
Execution
ControlsSlide6
Class Diagram
: This is the little beige boxes and arrows on the right. We’ll come back to them later.
World
Class
Diagram
Execution
ControlsSlide7
Execution Controls: includes the Act, Run, and Reset buttons, and the speed slider. More on them later.
World
Class
Diagram
Execution
ControlsSlide8
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.Slide9
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.Slide10
Exercise 1.1
: Create some wombats in the world.
Make some leaves too.Slide11
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.Slide12
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 WombatSlide13
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.Slide14
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
()Slide15
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.Slide16
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
?Slide17
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?Slide18
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 ParametersSlide19
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.Slide20
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)?Slide21
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 ControlsSlide22
Exercise 1.6Slide23
Exercise 1.6
Wombat Moves toward the LeavesSlide24
Exercise 1.6
Wombat Moves to LeavesSlide25
Example 1.7
Wombat Eats LeafSlide26
Exercise 1.8Slide27
Exercise 1.8
The >Act Execution Control Affects All the WombatsSlide28
Exercise 1.9
Wombat Runs Around the Edge of the WorldSlide29
Exercise 1.9
act()
method
If we’re sitting on a leaf, eat the leafOtherwise, if we can move forward, move forwardOtherwise, 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.Slide30
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.Slide31
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.Slide32
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.Slide33
One method is
populate()
.
Reset
the world, then try it out. Note the empty parameter list.Slide34
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.Slide35
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?Slide36
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
.Slide37
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
HumanSlide38
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.Slide39
Concept Summary