Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This lab is also available in PDF.
Summary: In this lab, we consider techniques for building objects, collections of data that support operations on those data.
Contents:
a. Make a copy of the make-switch
procedure from
the reading.
b. Test the switches created by the make-switch
procedure. Here are a few possible instructions.
> (define lamp-switch (make-switch)) > (define vacuum-cleaner-switch (make-switch)) > (lamp-switch ':show-position) > (vacuum-cleaner-switch ':show-position) > (lamp-switch ':toggle!) > (lamp-switch ':show-position) > (vacuum-cleaner-switch ':show-position) > (lamp-switch ':toggle!) > (vacuum-cleaner-switch ':toggle!) > (lamp-switch ':show-position) > (vacuum-cleaner-switch ':show-position)
As you may know, some switches have more than two positions. For
example, we might have a switch that switches between the values
0, 1, 2, and 3. (For a two-way light, 0 might represent off
,
1 might represent light one on
, 2 might represent light
two on
, and 3 might represent both lights on
.) In general,
you can only toggle to the next higher value (with 3 returning to 0
when toggled).
Implement four-stage-switch
, which responds to the
same messages as a switch, but with four possible stages.
Define a one-field object, tally
, that responds
to exactly four messages:
':show-contents
,
':set-contents-to-zero!
,
':increment!
, which has the effect of increasing the
number stored in the contents
field by 1.
':decrement!
, which has the effect of decreasing the
number stored in the contents
field by 1.
The initial value of the field should be 0.
For example,
> (tally ':set-contents-to-zero!) > (tally ':show-contents) 0 > (tally ':increment!) > (tally ':show-contents) 1 > (tally ':decrement!) > (tally ':decrement!) > (tally ':decrement!) > (tally ':show-contents) -2
Note that you are creating a single object, not a procedure that creates objects.
a. Define a make-tally
procedure that constructs and returns
objects similar to the tally
object you defined in exercise
2.
b. Create two tally objects and demonstrate that they can be incremented and reset independently.
Write a new make-tally
procedure that allows the client
to create new tallys with a specied initial value. For example, I
might say that a starting grade is 90 with
> (define grade (make-tally 90))
I would then increment and decrement it as students do good or bad work.
a. Define a constructor procedure, make-monitored-tally
, for
objects similar to the tally
objects from exercise 2 above,
except that each such object keeps track of the total
number of messages that it has received.
Hint: For this exercise, you may want to make two vectors, one for the value of the tally and one for the count of operations. Alternately, you could make a two-element vector in which element 0 of that vector is the value of the tally and element 1 of that vector is the count of operations.
b. Test your procedure.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/objects.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:25 2007.
The source to the document was last modified on Mon Apr 30 21:42:55 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/objects.html
.
You may wish to
validate this document's HTML
;
;
http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.