Week 29 · 4.5 Augmented reality and AI · 3 lessons
3
Project: shelter light
Computing 8: Topic 4.5, pp. 283–299
GoalPlan, make, test and evaluate an autonomous program for a bus shelter lamp.
New words
test plan · a table of tests with expected outcomes worked out before runningevaluate · say how well the solution works and how it could be betterinvalid data · data that should not be accepted, such as a sensor reading below 0
Explanation
Invented problem: a town wants a bus shelter lamp that turns on by itself, but only when it is dark and at least one person is waiting, to save power. Step 1: plan. List the inputs (a light sensor and a people counter), the decision and the output. Step 2: make. Write the program on paper. Step 3: test. Write a test plan with normal, boundary and invalid data, and work out each expected outcome before you run anything. Step 4: evaluate. Say what passed, what failed and how you would improve it. Then improve the program and test again.
Make: the first program. The lamp comes on when the light is below 30 and someone is waiting. With 20 and 2 typed the output is Shelter lamp on.
Test
Type of data
Test data
Expected outcome
Actual outcome
Pass/fail
1
normal
20, 2
Shelter lamp on
Shelter lamp on
pass
2
normal
80, 2
Shelter lamp off
Shelter lamp off
pass
3
normal
20, 0
Shelter lamp off
Shelter lamp off
pass
4
boundary
29, 1
Shelter lamp on
Shelter lamp on
pass
5
boundary
30, 1
Shelter lamp off
Shelter lamp off
pass
6
invalid
-5, 1
Sensor error
Shelter lamp on
FAIL
Test: the test plan. Expected outcomes were worked out first. Five tests pass. The invalid test (a sensor reading of −5) was expected to give a Sensor error, but the program says the lamp is on, because −5 < 30. That is a test failure.
Evaluate and improve: the lamp program works for sensible readings but does not check the sensor. The new version adds an invalid range check first, so a reading of -5 gives Sensor error. All six tests now pass. An even better version could tell the town to repair a faulty sensor.
Class activity
Your own project (on paper). Invented problem: a greenhouse window should open by itself when it is hot AND the wind is calm, so the plants do not get damaged. Plan: write the inputs, the decision and the output in a table. Make: write the program in Python or pseudocode, using a temperature of 0 to 50 and a wind speed from 0 to 100. Test: write a test plan with at least two normal tests, two boundary tests and one invalid test, with expected outcomes worked out before running. Evaluate: write three sentences about what failed and how to improve it. If you have a computer or tablet, ask an adult to help you run your program.
Practice
1
Write the expected outcome for these tests of the first shelter program: 10 and 3; 50 and 4; 29 and 0.
Light, people
Expected outcome
10, 3
?
50, 4
?
29, 0
?
10 and 3: lamp on. 50 and 4: lamp off. 29 and 0: lamp off, because nobody is waiting.
2
A boundary test of 30 light and 1 person is expected to turn the lamp on. Is the expected outcome correct? Explain.
Test data
Type
Expected outcome
30, 1
boundary
lamp on
No. The condition is light < 30, and 30 is not less than 30, so the lamp stays off. If the lamp should be on at 30, the program needs <= instead.
3
This version of the program has its checks in a different order. What does it print when light = −5 and people = 2? Is this better or worse than the improved program? Explain.
It prints Shelter lamp on, because −5 < 30 and 2 > 0, so the first branch runs and the sensor-error check is never reached. It is worse: in an if / elif chain only the first true branch runs, so the range check must come first.
4
Write two sentences to evaluate the shelter lamp program.
It works for normal and boundary data and saves power. It could be improved by also checking that the people counter is not negative, and by reporting a faulty sensor.