☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Review and your own mini-project

الأسبوع 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.

ABC(A∧¬B)∨C
0000
0011
0100
0111
1001
1011
1100
1111
Worked examples
Part of the planContent
InputsA: is someone inside? B: is it daylight? C: is the manual switch on?
OutputL: is the lamp on?
Rule in wordsOn when someone is inside and it is not daylight, or when the manual switch is on.
FormulaL = (A·¬B) + C
The plan in one table.
Php
1function lamp(bool $inside, bool $daylight, bool $manual): string {
2    return (($inside && !$daylight) || $manual) ? "on" : "off";
3}
4
5echo lamp(true, false, false), " ";
6echo lamp(true, true, false), " ";
7echo lamp(false, true, true), " ";
8echo lamp(false, false, false);
Output
on off on off
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
8
9Console.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.
Php
1for ($row = 0; $row < 8; $row++) {
2    $bits = str_pad(decbin($row), 3, "0", STR_PAD_LEFT);
3    echo $row, "  ", $bits, "  ", lamp($bits[0] === "1", $bits[1] === "1", $bits[2] === "1"), "\n";
4}
Output
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.
CaseA B CExpected L
someone reads at night, manual switch off1 0 0on
someone reads in daylight, manual switch off1 1 0off
nobody inside, daylight, manual switch on (cleaning)0 1 1on
nobody inside, night, manual switch off0 0 0off
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?
2
Which row number from 0 to 7 means A = 1, B = 0, C = 1?
3
What does the manual switch C do in the formula?
4
Give a test case with someone inside for which the lamp must be off.