The NOT simulator: one button flips the lamp
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 simulator | NOT simulator | |
|---|---|---|
| switches | two buttons | one button |
| start state | Off, Off, lamp unlit | On, lamp unlit |
| inner If | yes | no |
| the lamp is | decided by both texts | the opposite of the one text |
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
On -> unlit Off -> lit On -> unlit
| Step | Click | Text | Lamp |
|---|---|---|---|
| 0 | (start) | On | unlit |
| 1 | click | Off | lit |
| 2 | click | On | unlit |
| 3 | click | Off | lit |
| 4 | click | On | unlit |
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
On -> lit
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.