Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.

Similar presentations


Presentation on theme: "Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer."— Presentation transcript:

1 Lesson 1: Writing a program

2 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

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

4 Precise Language Let’s eat Grandpa! Let’s eat, Grandpa! Punctuation saves lives.

5 Precise Language Stop clubbing baby seals!

6 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

7 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

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

9 Describe a cat What are some properties of dogs? What are some sub-parts of a dog? What actions can a dog take?

10 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

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

12 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

13 Classes Let’s take a look Open the wombat “scenario” Double click to open the WombatWorld class

14 Let jump in and all some methods! Open Exercise 1 Let make the wombat move Double click Find the act() method and call move

15 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 turn Try: turn( 30 )

16 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

17 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).

18 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!

19 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

20 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

21 if( boolean_value ) doOneThing(); else doOneOtherThing(); If statements can also look like this Notice there are no braces and one command

22 Can we use if statements to 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

23 Can we use if statements to eat leaves?

24 What if we don’t want to move when we are eating? Or we only move when we are not eating?

25 Let’s add a sound for eating If your computer has a microphone, you can record a sounds Go to the menu “Controls > Show sound recorder” Click record

26 Let’s add a sound for eating Run a search for “eating wav” “.wav” is the type sound file we would like Websites like soundbible.com and www.freesound.org have free sounds.www.freesound.org Find a sound you like Download it Place it in the “lesson 1\sounds” folder

27 Let’s add a sound for eating Now we just have to call Greenfoot.playSound( “filename.wav” ); For example I have provided a chomp chomp sounds. Try Greenfoot.playSound( “ChompChompChomp.wav” );

28 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

29 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

30 Examples Type/Value – Number Age 10 – Number OtherNumber 12 – Decimal numberPi3.14 – Text Preferences ”I like monkeys”

31 Example in Java int age= 10; int otherNumber = 12; double Pi = 3.14; String preferences = “I like monkeys”;

32 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 Personmartin (Create a new Person Called Martin) Actorprofessor martin List of LeavesremainingLeaves (list of remaining leaves) ActorkingOfCanadaNone;

33 Example in Java // Assume a Person is a type of actor Person martin = new Person( “Martin” ); Actor professor = martin; List remaingLeaves = getObjects(Leaf.class); Actor kingOfCanada = null;

34 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

35 Stopping the game Lists 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”

36 Try your own ideas Add a sound at the end of the game Add rocks to the World (see populate()) Make it so you cannot walk over rocks or walk more slowly Add a second wombat, create a second Actor Give it different controls Try “left”, “up” “down”, “right” (the arrows)


Download ppt "Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer."

Similar presentations


Ads by Google