lesson 1: writing a program. java java is a programming language computer cannot understand it,...
TRANSCRIPT
![Page 1: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/1.jpg)
Lesson 1: Writing a program
![Page 2: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/2.jpg)
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
![Page 3: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/3.jpg)
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.
![Page 4: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/4.jpg)
Precise Language
• Let’s eat Grandpa!Let’s eat, Grandpa!
Punctuation saves lives.
![Page 5: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/5.jpg)
Precise Language
• Stop clubbing baby seals!
![Page 6: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/6.jpg)
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
![Page 7: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/7.jpg)
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
![Page 8: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/8.jpg)
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?
![Page 9: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/9.jpg)
Describe a cat
• What are some properties of dogs?
• What are some sub-parts of a dog?
• What actions can a dog take?
![Page 10: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/10.jpg)
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
![Page 11: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/11.jpg)
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.
![Page 12: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/12.jpg)
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
![Page 13: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/13.jpg)
Classes
• Let’s take a look
• Open the wombat “scenario”
• Double click toopen the WombatWorldclass
![Page 14: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/14.jpg)
Let jump inand all some methods!
Open Exercise 1
Let make the wombat move
Double click
Find the act() method and call move
![Page 15: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/15.jpg)
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 )
![Page 16: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/16.jpg)
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
![Page 17: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/17.jpg)
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).
![Page 18: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/18.jpg)
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!
![Page 19: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/19.jpg)
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
![Page 20: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/20.jpg)
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
![Page 21: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/21.jpg)
if( boolean_value )doOneThing();
elsedoOneOtherThing();
If statements can also look like this
Notice there are no braces and one command
![Page 22: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/22.jpg)
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
![Page 23: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/23.jpg)
Can we use if statementsto eat leaves?
![Page 24: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/24.jpg)
What if we don’t want to move when we are
eating?Or we only move when we are not eating?
![Page 25: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/25.jpg)
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
![Page 26: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/26.jpg)
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
![Page 27: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/27.jpg)
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” );
![Page 28: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/28.jpg)
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
![Page 29: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/29.jpg)
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
![Page 30: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/30.jpg)
Examples
• Type/Value – Number Age 10– Number OtherNumber 12– Decimal number Pi 3.14– Text Preferences ”I like monkeys”
![Page 31: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/31.jpg)
Example in Java
int age= 10;int otherNumber = 12;double Pi = 3.14;String preferences = “I like monkeys”;
![Page 32: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/32.jpg)
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;
![Page 33: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/33.jpg)
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;
![Page 34: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/34.jpg)
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
![Page 35: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/35.jpg)
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”
![Page 36: Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697bf8e1a28abf838c8d07b/html5/thumbnails/36.jpg)
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)