






Hotspots: Admin Pages | Turn-in Site |
Current Links: Case Final Project Summer 2007
Sp01 Final Exam Review: Sensor Management System
See Final Exam Review - Sp2001
Comments? Answers? Criticisms?
a.
numbersRead _ 0.
[numbersRead < 100] whileTrue: [
command _ SensorStream next.
numbersRead _ numbersRead + 1.
(command = 0) ifTrue: [
x _ SensorStream next.
y _ SensorStream next.
str _ (Character value: (SensorStream next)) asString.
str displayAt: (x@y).
numbersRead _ numbersRead + 2.
].
].
b.
soundalarms _ Bag new.
numbersRead _ 0.
[numbersRead < 100] whileTrue:[
command _ SensorStream next.
numbersRead _ numbersRead + 1.
((command >= 1) and: [command <= 255]) ifTrue: [
soundalarms add: command.
].
((command >=256) and: [command <=511]) ifTrue: [
count _ soundalarms occurrencesOf: (command-255).
(count > 3) ifTrue: [
command inform: 'ALARM!'.
].
].
].
- I chose to use a Bag to record and quickly find out how many times a soundsensor had been triggered. A Bag is a type of Collection that remembers the number of identical elements that it holds. A Bag is fast because it stores its contents in a Dictionary. The Object provides a hash value for where the number of occurences should be stored in the Dictionary. Thus, lookup time is O(1).
Doug Powers
a. "numbersRead _ numbersRead + 2." should be "3", not "2"
b. "(count > 3) ifTrue: [" should be ">=", not ">"
chok :-)
You're correct on the 3 instead of 2 thing; however I think the > is still correct since it says "more than".
Doug Powers
oops! you're right, sorry.
chok :-)
puzzeld whats a bag got to do with it
Bag has an occurencesOf: method which is very fast. If you use an OrderedCollection, instead, then occurencesOf: is linear. If you use a Set, then each number will only be stored once, which is not what we want here. -Lex Spoon
very intresting but does it work?
Link to this Page