Week 28 · Unit 12: Be a problem solver · 3 lessons
3
From button to flowchart
Computing 6: Unit 12, pp. 162–177
GoalTurn a requirement into a flowchart and Scratch blocks for a button, then test it against the design.
New words
button sprite · a sprite that does something when the user clicks itflowchart · a diagram that shows the steps of an algorithmvariable · a named place where a program keeps a valuetest · to run a program with chosen inputs to check that the results are right
Explanation
Every button is a small algorithm. For the CAKE button the steps are: when it is clicked, it switches to its pressed costume and makes a click sound, asks how many, stores the answer in a variable, sends a message and switches back. Another script receives the message and does the calculation. We draw the flowchart first and then build the blocks to match it. After building, we test two things. Does the screen look the way it was designed? Does the code work for different inputs? To find a fault we compare the expected result with the actual result. Then we change the prototype and test again.
Worked examples
This is the Scratch script that matches the flowchart. Each block matches one step.
Here the message is received by a second script. A cake costs 3, so the total goes up by cakes × 3. Trace it: the total starts at 0 and the customer buys 4 cakes and then 5 biscuits that cost 1 each.
Step
What the user does
total after
start
—
0
1
presses CAKE, types 4
0 + 4 × 3 = 12
2
presses BISCUIT, types 5
12 + 5 × 1 = 17
The trace table gives the total after each button. A test table like this is compared with the expected results. Here 4 cakes cost 12 and 5 biscuits cost 5.
Class activity
Act the program (no computer): four players. The Button reads its card aloud, asks the Customer ‘how many cakes (1 to 8)?’ and writes the answer on a card named cakes. Then the Button shouts ‘add cakes!’ The Total-keeper has a card ‘when I hear add cakes: total = total + cakes × 3’ and writes the new total on a board. Run three customers and check each total with a calculator. Then give the Total-keeper a bug card, ‘total + cakes + 3’, and compare. Which test would have found the bug first? If you have a computer or tablet, build the button in Scratch.
Practice
1
Use the scripts in example 2. The total starts at 0. A customer buys 4 cakes and then 6 biscuits. Fill in the trace table.
Step
What the user does
total after
start
—
0
1
presses CAKE, types 4
?
2
presses BISCUIT, types 6
?
After step 1 the total is 0 + 4 × 3 = 12. After step 2 it is 12 + 6 × 1 = 18.
2
Describe a flowchart for a NEW CUSTOMER button that sets the total to 0 and says Ready for 2 seconds.
Start, then ‘user clicks NEW CUSTOMER’, then ‘set total to 0’, then ‘say Ready for 2 seconds’, then End.
3
A customer buys 5 cakes. The total should be 15, but it shows 5. Find the bug and write the correct block.
The block does not multiply by the price, so only the number of cakes is added. The correct block is change [total v] by ((cakes) * (3)).
4
The prompt asks for 1 to 8, but the user types 25. What could the designer add to the program?
A check with an if block: if the answer is less than 1 or more than 8, say ‘Please type a number from 1 to 8’ and ask again.