Week 29 · Unit 12: Be a problem solver · 3 lessons
2
Plan a small program
Computing 6: Unit 12, pp. 162–177
GoalFollow the stages to plan, build and test a small Scratch program with a loop and an if–else block.
New words
requirement · something that the program must doalgorithm · a set of steps to solve a problemtest plan · a table of tests with inputs, expected results and actual resultsboundary value · a test value on the edge of a rule, such as exactly 50 when the rule is 50 or more
Explanation
Here is a whole project for a savings jar program. We follow the stages in order. First, the requirements: the program asks three times how many coins were saved, it keeps a running total, it starts from 0 each time it runs, and at the end it says whether the goal of 50 was reached. Second, design: the flowchart uses a counter and a loop, and an if–else block at the end. Third, implementation: the blocks follow the flowchart. Fourth, testing: the test plan has normal values and boundary values around 50. Last, evaluation: we tick every requirement. Because a boundary can hide a bug, the test of exactly 50 is the most important.
Number
Requirement
R1
asks three times: How many coins today?
R2
keeps a running total
R3
the total starts at 0 on every run
R4
at the end it says Goal reached! for 50 or more, or Keep saving
Worked examples
This is the design stage. The flowchart counts the rounds with a variable called rounds.
This is the implementation. Scratch has no ‘50 or more’ block, so the test total ≥ 50 from the flowchart becomes total > 49. The loop repeats three times, so we do not need a rounds variable in Scratch. With the answers 20, 15 and 20 the total is 55, so the program says Goal reached!
Inputs
Total
Expected
Actual
Result
10, 10, 10
30
Keep saving
Keep saving
pass
25, 25, 0
50
Goal reached!
Goal reached!
pass
25, 24, 0
49
Keep saving
Keep saving
pass
0, 0, 0
0
Keep saving
Keep saving
pass
This is the testing stage. The test plan includes the boundary at 50. The values just below and on the edge are the most useful tests.
Class activity
Paper project (no computer): choose a program with an if–else block, for example a program that says ‘Take an umbrella’ if the chance of rain is 50 or more, and ‘No umbrella’ if not. On one sheet write the five headings of the project plan. Write three requirements, draw the flowchart and write the blocks on paper strips. Your partner is the computer: give them three test inputs, including the boundary, and let them run your strips by hand. Fill in a test table. Did every test pass? If you have a computer or tablet, build the program in Scratch.
Practice
1
Trace the program for the answers 12, 8, 15 and 4. What does it say?
The total goes 12, 20, 35 and 39. Since 39 > 39 is false, it says Keep collecting.
2
For the sticker program, write three tests with totals around the boundary of 40. Give the expected result of each.
Total 39: Keep collecting. Total 40: Album full! Total 41: Album full!
3
Write two requirements for a program that tells a user whether they have enough money for a snack that costs 5 coins.
For example: the program asks how many coins the user has. If the answer is 5 or more it says ‘You can buy it’, and otherwise it says ‘Not enough coins’.
4
Look at the test results. The test with a total of exactly 50 says Keep saving, but the expected result is Goal reached!. What is the likely bug?
Test
Total
Expected
Actual
1
49
Keep saving
Keep saving
2
50
Goal reached!
Keep saving
3
51
Goal reached!
Goal reached!
The condition probably uses total > 50, so a total of exactly 50 fails. It should be total > 49 (50 or more). Change it and test again.