View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Sp02 Final Exam Review: Sensor Management System

Something to note. you can't actually see every number in the range of [0,65536] if your representation is only 2 bytes. [0,65536) is more accurate, or [0,65536] - {x | x > 0 ^ x < 65536 ^ x is contained in Z}. Shaggz The little carrots represent logical and, of course.


a.
i _ 1.
(i = 100) whileTrue:
[(SensorStream next = 0) ifTrue:
[x := SensorStream next.
y := SensorStream next.
((Character value: (SensorStream next)) asString) displayAt: (Point x: x y: y)
i _ i + 3.]
i _ i + 1.]

The index i is needed here because the wording of the problem says only to use the first 100 readings.

b.

b := Bag new.
100 timesRepeat: [x := SensorStream next.
(x >= 1) ifTrue:
[(x = 255) ifTrue:
[ b add: x.]].
(x >= 256) ifTrue:
[ (x = 511) ifTrue:
[((b occurrencesOf: x) > 3) ifTrue: [
b inform: 'ALARM!']]]].

The data structure I used was a bag, because it allows addition of the same object more than once, and can give the number by the message occurrencesOf.

Hank Wilde

How can you do (b occurrencesOf: x) if you never added any x > 255

My revised answer, indented and with comments:

b := Bag new.
100 timesRepeat:
[x := SensorStream next.
(x >= 1) ifTrue:
[(x = 255) ifTrue: "sound sensor has gone off"
[ b add: x.]].
(x >= 256) ifTrue:
[(x = 511) ifTrue: "motion sensor has gone off"
[((b occurrencesOf: x-255) > 3) ifTrue:
[b inform: 'ALARM!']]]].

So, the line that changed was (b occurrencesOf: x - 255) in order to see how many times the corresponding sound sensor went off.

Hank Wilde


Link to this Page