Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | gerard-charles |
View: | 218 times |
Download: | 0 times |
www.computerscienceuk.com
Quick Review of Last Lesson
Event Driven Programming
Blocks of code can be run when certain events occur
Random Function
In Scratch there is a function to pick a random number
www.computerscienceuk.com
• Learning Objectives– Remember what is meant by “event-driven” computer
programming– Remember how to generate a random number in scratch– Understand what a variable is– Understand how computer make decisions (IF
Statements
• Lesson Outcomes– Produce a Magic 8-Ball program
Literacy – Key Words
Variable A place where programs store data – think of them as storage boxes.
IF Statement The code needed to get programs making decisions.
www.computerscienceuk.com
Variables• If you were playing a game
and rolled a dice, you would need to memorize the number and then use the number to move your counter around the board.
• When programs work with data they need to memorize things too (even if its just for a second).
• Programs don’t have brains to store their data, instead they have variables.
I rolled a 6 – I must
remember that so I can move my counter
?
www.computerscienceuk.com
What is a variable?
• In programming, a variable can be thought of as a storage box.
• The box may be given a name, and it may hold various different things.
• In scratch, is the name of a ready made variable, and it will store what ever you type into scratch.
Variable called ‘ANSWER’ Contents is anything we type in
Storage
www.computerscienceuk.com
Creating New VariablesBut we can also create our own storage boxes (variables)
To create a variable we:- click on the
variables button
- Make a variable
- Give it a name
We can then set the box to a particular value – in other words“put some data in the
box”
www.computerscienceuk.com
Variables - Summary
Variables are the name of the boxContents is any data the program needs to keep hold of
Variables are storage boxes which programs use to hold/store data
Programmers can create as many variables as they like.
www.computerscienceuk.com
IF statements are how programs make decision.
IF this occurs, I will do this
ELSE, I will do that
IF – ELSE statements allow programs to make decisions based on certain conditions occurring.
www.computerscienceuk.com
The IF Statement
What is this script going to do?When we click the sprite a random number is generated and
stored in the variable called ‘number’. If number = 1, the cat will say “ your random number is 1”.
If number = 2 the cat will say “ your random number is 2”.
www.computerscienceuk.com
Magic Eight Ball• You shake
them and they give you a random message – they are fortune tellers!
…and you are now going to program one!
www.computerscienceuk.com
Planning before we program
• Before programmers start programming, they always start by imagining the program as a real life object, as a human perhaps.
• If you were a magic 8 ball what would you do?
1. Wait to be shaken2. Pick a random statement3. Say the random statement
Write down your thoughts on a whiteboard
www.computerscienceuk.com
If you were the 8-ball program, what would you
do?• Wait for user to shake 8-ball
(Event / Input)• Random number would then
be generated• You would memorize the
number (store it)• You would decide what the
number means (process it)• You would then output a
message based on that random number
INPUT
STORE NUMBER IN VARIABLE
What does a 1 mean?
OUTPUT Message
www.computerscienceuk.com
Planning = AlgorithmRandom number would then be generated
You would memorize the number (store it)
HOW?
STORE NUMBER IN VARIABLE
www.computerscienceuk.com
Planning = AlgorithmYou would decide what that means (process it)
HOW?
What does a 1 mean?
www.computerscienceuk.com
Planning = AlgorithmYou would then output a message based on that random number
HOW?
OUTPUT Message
www.computerscienceuk.com
Task
• Create your own Magic 8-Ball program– It must have an image which the user can
click–When its clicked it must select a random
statement and display it on the screen
• Extend by developing the look of the program – improve the image, background or even animate the ball.