AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Prototypes and screens

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.

Startuser clicks the CAKE buttonshow the pressed costume, play a clickask: How many cakes (1 to 8)?set cakes to the answerbroadcast add cakesshow the normal costumeEnd
Worked examples
when this sprite clickedswitch costume topressedstart soundPopaskHow many cakes (1 to 8)?and waitsetcakestoanswerbroadcastadd cakesswitch costume tonormal
This is the Scratch script that matches the flowchart. Each block matches one step.
Cake pricewhen I receiveadd cakeschangetotalbycakes*3Biscuit pricewhen I receiveadd biscuitschangetotalbybiscuits*1
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.
StepWhat the user doestotal after
start—0
1presses CAKE, types 40 + 4 × 3 = 12
2presses BISCUIT, types 512 + 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.
StepWhat the user doestotal after
start—0
1presses CAKE, types 4?
2presses BISCUIT, types 6?
2
Describe a flowchart for a NEW CUSTOMER button that sets the total to 0 and says Ready for 2 seconds.
3
A customer buys 5 cakes. The total should be 15, but it shows 5. Find the bug and write the correct block.
Cakeswhen I receiveadd cakeschangetotalbycakes
4
The prompt asks for 1 to 8, but the user types 25. What could the designer add to the program?