الأسبوع 15 · مراجعة الفصل الدراسي الأول (الوحدتان ١ و٢) ومشروع صغير من تصميمك · 3 دروس
3
Your own mini-project: a three-condition rule, built and tested
ICT Secondary 3 Term 1, Review: pp. 5–86
GoalPlan, tabulate, code and test a lamp rule with three conditions: L = (someone inside AND NOT daylight) OR manual switch.
New words
mini-project · a small complete task: plan, rule, table, code and teststest table · a table of inputs with the output you expect, used to check a programrow number · the number 0 to 7 of a row; written in binary it gives the values of A, B and Cinvalid input · a value that is missing or is not a clear yes or no
Explanation
Here is Darslab’s own mini-project: the lamp of an invented library reading corner. Step 1, the plan. The inputs are three yes-or-no questions: A, is someone inside? B, is it daylight? C, is the manual switch on? The output is L, is the lamp on? Step 2, the rule in words: the lamp is on when someone is inside and it is not daylight, or when the manual switch is on. The formula is L = (A·¬B) + C. Step 3, the table. Three inputs make 2³ = 8 rows. The row numbers 0 to 7, written in binary, are exactly the columns A, B and C, as the bit patterns of Week 2: row 5 is 101, so A = 1, B = 0, C = 1. The part A·¬B is 1 only for the rows 100 and 101, and C is 1 for the rows 001, 011, 101 and 111, so five rows have L = 1. Step 4, the code, in PHP and in VB.NET, and the two must give the same table. Step 5, the tests: a short test table with the output you expect. One more decision belongs to the plan: if a condition is missing or is not a clear yes or no, the program does not guess. It reports invalid input and does not change the lamp.
A
B
C
(A∧¬B)∨C
0
0
0
0
0
0
1
1
0
1
0
0
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
0
1
1
1
1
Worked examples
Part of the plan
Content
Inputs
A: is someone inside? B: is it daylight? C: is the manual switch on?
Output
L: is the lamp on?
Rule in words
On when someone is inside and it is not daylight, or when the manual switch is on.
The PHP function and four calls. The output line gives the lamp for (inside, daylight, manual) = (true, false, false), (true, true, false), (false, true, true) and (false, false, false).
Vb
1Function Lamp(inside As Boolean, daylight As Boolean, manual As Boolean) As String
2 If (inside And Not daylight) Or manual Then
3 Return "on"4 Else
5 Return "off"6 End If
7End Function
89Console.WriteLine(Lamp(True, False, False) & " " & Lamp(True, True, False) & " " & Lamp(False, True, True) & " " & Lamp(False, False, False))
Output
on off on off
The same function in VB.NET, inside a small console program. The output line is the same four answers.
0 000 off
1 001 on
2 010 off
3 011 on
4 100 on
5 101 on
6 110 off
7 111 on
All eight rows from the PHP function, with the row number, the row number in binary (the values of A, B and C) and the lamp. The three characters of $bits are A, B and C.
Case
A B C
Expected L
someone reads at night, manual switch off
1 0 0
on
someone reads in daylight, manual switch off
1 1 0
off
nobody inside, daylight, manual switch on (cleaning)
0 1 1
on
nobody inside, night, manual switch off
0 0 0
off
A test table for the library corner. Each case has the expected lamp.
Class activity
Add a fourth condition D, “the library is closed”, so that the lamp is always off when D is 1. Write the new rule in words and the new formula, and say how many rows the table has now.
Practice
1
How many rows has the table of the three-condition rule, and how many have L = 1?
8 rows; 5 of them have L = 1.
2
Which row number from 0 to 7 means A = 1, B = 0, C = 1?
Row 5, written 101.
3
What does the manual switch C do in the formula?
Whenever C is 1 the lamp is on, whatever A and B are.
4
Give a test case with someone inside for which the lamp must be off.
Someone inside, daylight, manual switch off: 1 1 0, so the lamp is off.