Upload
ginger-merritt
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
Lesson 1: Writing a program
Java
• Java is a programming language• Computer cannot understand it, though it can
be translated (compiled) so a computer can understand it
• Computers only understand precise and correct language; they are more picky than your English or French teachers
Java
• Like with other languages (English, French, Spanish) you must learn the words, meaning, and syntax.
• You must correctly express yourself or the computer will misunderstand.
• Little details matter. Where you put a semi-colon can change the meaning.
Precise Language
• Let’s eat Grandpa!Let’s eat, Grandpa!
Punctuation saves lives.
Precise Language
• Stop clubbing baby seals!
Structure of a program
• Programs are broken into classes– Actors in Greenfoot
• Classes have methods– Stuff they can do
• Classes have properties– Details that describe them
• Methods have instructions– What to do and when to do them
Classes
• Classes represent idea or type of thing– Example: cups, humans, wombats, walls
• A class generically describes the type– Humans have a name, 2 arms, age, height, etc.
• A class defines generic actions– Humans can walk, eat, sleep, turn
• A class can be a sub type– A human is an animal, a dog is an animal, a cup is NOT an animal
Describe a dog
• What are some properties of dogs?
• What are some sub-parts of a dog?
• What actions can a dog take?
• A dog a special type of something?– Are there special types or different types of dogs?
Describe a cat
• What are some properties of dogs?
• What are some sub-parts of a dog?
• What actions can a dog take?
Methods
• Classes can have methods (commands) that can be given to anything of this type
• These are like set of instructions to accomplish something
• Example telling any human to walk• Example telling a dog to sit
Methods
• Make ice cream– Place 2 cups whipping cream, 1 cup milk, ¾ a cup
of sugar, a little vanilla in a bowl.– Mix the ingredients in bowl– Place in ice cream maker to freeze contents.
Methods
• Define word (eg. Define gregarious)– If I know the meaning• Tell professor
– Otherwise if I have internet access• Look it up online• Tell professor
– Otherwise• Find a dictionary• Look it up• Tell professor
Classes
• Let’s take a look
• Open the wombat “scenario”
• Double click toopen the WombatWorldclass
Let jump inand all some methods!
Open Exercise 1
Let make the wombat move
Double click
Find the act() method and call move
But he just gets stuck!Each time the game updates the act method is
called.So the wombat just keeps moving forward as the
game progresses.
Can we make him turn with the turn method?turn requires a number of degrees to turnTry: turn( 30 )
Now he goes in circles.That’s not fun!
Can we use methods to control him?Yes. We will ask Greenfoot what keyboard button is
pressed, then do something.
We will tell the computer if( question ) then something if( “a” is pressed down ) then turn and go left
Now let’s add controls for all directions!
We will use a for left (180), w for up (270 or -90), s for down (90), and d for right (0).
Calling a method
method_name( argument1, argument2 );
Example of commands:turn( 90 );turn ( 180 ) ;move(1 );
Some spaces don’t matter, but the CAPITAL or lowercase letters do.
A command/statement ends in a semi-colon!
If statements
• If statements are used to control the flow of a program– What part do we want to run when?– Just like we only want the wombat to turn when a
key is pressed
• The question must be true or false– It computer terms we call this a boolean value
If statements
if( boolean_value ){
// Do something when boolean_value = true// Put as many instructions as you want between funny braces { }
}else{
// do something when boolean_value = false}
Notice there are no semi-colons
if( boolean_value )doOneThing();
elsedoOneOtherThing();
If statements can also look like this
Notice there are no braces and one command
Can we use if statementsto eat leaves?
Let’s make the wombat eat leaves
We will use the foundLeaf() and eatLeaf() methods already in the wombat.
foundLeaf return a boolean value if the wombat is on a leaf
Can we use if statementsto eat leaves?
What if we don’t want to move when we are
eating?Or we only move when we are not eating?
Let’s add a sound for eating
If your computer has a microphone, you can record a soundsGo to the menu “Controls > Show sound recorder”Click record
Let’s add a sound for eating
Run a search for “eating wav”“.wav” is the type sound file we would likeWebsites like soundbible.com and
www.freesound.org have free sounds.
Find a sound you likeDownload itPlace it in the “lesson 1\sounds” folder
Let’s add a sound for eating
Now we just have to callGreenfoot.playSound( “filename.wav” );
For example I have provided a chomp chomp sounds. TryGreenfoot.playSound( “ChompChompChomp.wav” );
Objects, Variables, and Primitives
• How do we store values, objects, and information for later use?– Number of leaves eaten– Position– Lists of leaves
• How do we update information?– Increase the number of leaves eaten– Removing a leaf from a list
Variables
• We use a variable to store information
• A variable as a type, name, and value– Type is what kinds of things it can store– Name is used to indicate which information we
want– Value is the specific thing it is currently store
Examples
• Type/Value – Number Age 10– Number OtherNumber 12– Decimal number Pi 3.14– Text Preferences ”I like monkeys”
Example in Java
int age= 10;int otherNumber = 12;double Pi = 3.14;String preferences = “I like monkeys”;
References
• When you refer to an Object or Actor you must use a reference.– You don’t store the Actor, you refer to them– You want the actor to keep doing their job– References are like other variables
Person martin (Create a new Person Called Martin)Actor professor martinList of Leaves remainingLeaves (list of remaining leaves)Actor kingOfCanada None;
Example in Java// Assume a Person is a type of actorPerson martin = new Person( “Martin” );Actor professor = martin;
List<Leaf> remaingLeaves = getObjects(Leaf.class);
Actor kingOfCanada = null;
Stopping the game Let’s stop the game when all the leaves are eaten
Open the WombatWorld Code
Add an public void act() method
Get the remaining list of the Leaves
If there are none, stop
Stopping the gameLists have many built in methods
isEmpty return a boolean (true or false) depending on if the list is empty (contains nothing)
We can find documentation by searching for“Java List”
Try your own ideasAdd a sound at the end of the game
Add rocks to the World (see populate())
Make it so you cannot walk over rocksor walk more slowly
Add a second wombat, create a second ActorGive it different controls
Try “left”, “up” “down”, “right” (the arrows)