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

Sum00 Final Exam Review: Sensor Management System

Edit Sum00 Final Exam Review: Sensor Management System here.

a)

tmp x y c i
i _ 1.
i to: 100 do:
    [tmp _ SensorStream next.
      (tmp = 0)
        ifTrue: [((i + 3) = 100)
          ifTrue: [x _ SensorStream next.
              y _ SensorStream next.
              c _ Character value: (SensorStream next).
              (c asString) displayAt: (x @ y).].
     ].
      i _ i + 1.
].

Daniel Phommathep

b)

i tmp S o

o _ Object new.
i _ 1.
S _ Array new: 255.

100 timesRepeat: [:j | S at: j put: 0.].

i to: 100 do:
  [tmp _ SensorStream next.

  (tmp >= 1 & tmp = 255)
    ifTrue: [S at: tmp put: ((S at: tmp) + 1).].

  (tmp >= 256 & tmp = 511)
    ifTrue: [((S at: (tmp - 255)) >= 3)
      ifTrue: [o inform: 'ALARM'.].
  i _ i + 1.
  ].

I chose to use an array because you can directly access elements in it without traversing through the entire structure. Whenever a sound sensor is triggered, you just increment the value at the index corresponding to the number returned from the SensorStream (i.e. if the stream returned 4, the value at index 4 would get incremented by 1). This way, if the value returned is in the motion sensor range, you can just subtract 255 from that value and access the array at the calculated index and check to see if it is greater than or equal to 3.

Daniel Phommathep


How about a solution that doesn't do 1 to: 100 do:? Mark Guzdial



You could do...
i := 1.
[i (less than or equal to) 100] whileTrue: [...]

Sorry, I couldn't get a less than sign to show up, for some reason.
nobody
Because it's HTML <sigh>. It's a little trickier than that, Bill, because you have to increment on the right conditions so as to read only 100 elements... Mark Guzdial



b.) Use a bag and add stuff to the bag with the add: command (you'll be adding the numbers. i.e. myBag add: 1;add:2 etc.)
then when you want to find out how many of 1 or two you have use bags nifty method occurencesOf: to find out how many of that object you have.
But it's one or two IN ORDER – sequentiality is lost in a Bag. Mark Guzdial




Link to this Page