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

Sum01 Final Exam Review: Sensor Management System

I'll take a shot at this.

a) I'll assume that the last three messages of a steam don't begin
a new Character Display Message
0_10_100_65 -> put 'A' at 10@100

    | value x y char count |
    count := 0.
    (count < 98) while:[ "won't look past the 98th"
        value := SensorStream next.
        (value = 0) 
            x := SensorStream next.
            y := SensorStream next.
            char := Character value: (SensorStream next).
            char displayAt: ( Point x: x y: y). "I forget how point works"
           count := count + 4.
        ifFalse:[ count := count + 1.]. 
      ]. "while"

| soundSensors value count |
soundSensors := Array new.
1 to: 100 do: [
    value := SensorStream next.
    (value >= 1) and: [value < 256]
        count := soundSensors at: value.
        (count = nil) ifTrue: [ count := 1]
                      ifFalse: [count := count + 1].
    (value >= 256) and: [value < 511]
         value := value - 256.
         ( soundSensors at: value) != nil ifTrue: [
             ( soundSensors at: value > 3) ifTrue: [
               Object inform: ('trouble in room ', value asString).

yeah, but I haven't complied this, so I might be completly off.

dang, it lost all of my formatting. Check out the HTML tag for Swiki, which I've now inserted! -Lex Spoon

Does anyone have a comment or an alternative suggestion? -Lex Spoon
(value >= 256) and: [value = 511]
value := value - 255.
Also, need to "put" count in the Array...

Good comments. Want to try the whole routine again?

Other comments:

On (b), everyone is overlooking some built-in collection classes that help count occurrences of things. There is a collection for "seen-it vs. not-seen-it" and there is another that counts the number of times an item has been seen.

On (b), both kinds of sensors are combined into a single array. This will work, though the name of the array should really be "sensors", not "soundSensors". However, why combine them? The external data is combined, and there is only so much effort that one question is worth, but in this class, it is still worth thinking of ways to represent the data in a way that is convenient internally. In the English description of the text you see things like "256 indicates motion sensor 1". But in the code, 256 means absolute sensor 256, which must then be decoded again.

Finally, there is an easier way to repeat an operation 100 times.... -Lex Spoon

I assume you are talking about Bag collections? They have a O(1) access time, correct?
I'll take a stab at this one:

value sensors count

sensors := Bag new.
100 timesRepeat:
[value := SensorStream next.
(value > 0) and: [value 256]
ifTrue: [sensors add: value.]
ifFalse: [((value > 255) and: [value = 511])
ifTrue: [value := value - 255.
(sensors includes: value)
ifTrue: [count := (sensors occurrencesOf: value).
(occurrences > 3)
ifTrue: [Object inform: 'ALARM'.].

Link to this Page