☰ Year plan · Communication Technology

The NOT gate

Week 13 · Unit 2 Topics 7–9: the NOT gate and its simulation projects with VB.NET and PHP · 3 lessons
2

The NOT simulator: one button flips the lamp

ICT Secondary 3 Term 1, Unit 2, Topics 7–9: pp. 65–81
GoalRead the code of the NOT simulator, trace its clicks, and find a bug.
New words
start state · the state when the window opens: here the text On and the lamp unlitflip · change to the other of two valuesopposite · the reverse value: the lamp shows the opposite of the switchbug · a mistake in a program that makes it behave wrongly
Explanation

The NOT simulator has one button, SwitchA, and the lamp box. The Load code starts with the text On and the lamp unlit, because NOT 1 is 0. The click code flips the text. If the text is Off it becomes On and the lamp is unlit. Otherwise it becomes Off and the lamp is lit. The lamp always shows the opposite of the text: On, unlit; Off, lit. Compare it with the AND and OR simulators. There is only one switch, so there is no other switch to look at, and there is no inner If. The helper ShowLamp from Week 9 is the same. To trace the code, write the table of clicks as before. Because one click flips the text, the states repeat in pairs: after an odd number of clicks the text is Off and the lamp is lit, and after an even number it is On and the lamp is unlit. A bug is easy to make by copying a line: if both branches call ShowLamp(True), the lamp stays lit when the text returns to On, which breaks the NOT rule.

AND or OR simulatorNOT simulator
switchestwo buttonsone button
start stateOff, Off, lamp unlitOn, lamp unlit
inner Ifyesno
the lamp isdecided by both textsthe opposite of the one text
Worked examples
Vb
1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
2    SwitchA.Text = "On"
3    ShowLamp(False)
4End Sub
5
6Private Sub SwitchA_Click(sender As Object, e As EventArgs) Handles SwitchA.Click
7    If SwitchA.Text = "Off" Then
8        SwitchA.Text = "On"
9        ShowLamp(False)
10    Else
11        SwitchA.Text = "Off"
12        ShowLamp(True)
13    End If
14End Sub
Output
On -> unlit
Off -> lit
On -> unlit
The Load and click code of the NOT simulator. The output lines are the state after the window loads, after the first click and after the second click.
StepClickTextLamp
0(start)Onunlit
1clickOfflit
2clickOnunlit
3clickOfflit
4clickOnunlit
A trace of four clicks from the start state. The states repeat in pairs.
Vb
1Private Sub SwitchA_Click(sender As Object, e As EventArgs) Handles SwitchA.Click
2    If SwitchA.Text = "Off" Then
3        SwitchA.Text = "On"
4        ShowLamp(True)
5    Else
6        SwitchA.Text = "Off"
7        ShowLamp(True)
8    End If
9End Sub
Output
On -> lit
A bug hunt. Here both branches show the lit lamp. The output line is the state after two clicks, when the text is On again but the lamp should be unlit.
Class activity

Trace the NOT simulator for five clicks from the start state and write the table. Then say what the simulator shows after 25 clicks. Use the pattern; do not write 25 rows.

Practice
1
What are the start text and the start lamp of the NOT simulator?
2
Why does the NOT click code have no inner If?
3
After three clicks from the start state, what does the simulator show?
4
What is wrong with a click code that shows the lit lamp in both branches?