Fundamentals of Computer Science I (CS151 2003F)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
Summary: In this lab, we consider techniques for building objects, collections of data that support operations on those data.
Contents:
Start DrScheme.
Here's the make-switch procedure from
the reading.
;;; Procedure:
;;; make-switch
;;; Parameters:
;;; None
;;; Purpose:
;;; Creates a new switch in the off position.
;;; Produces:
;;; newswitch, a switch
;;; Preconditions:
;;; None
;;; Postconditions:
;;; newswitch is an object which responds to two messages:
;;; :show-position
;;; Shows the current position ('on or 'off)
;;; :toggle!
;;; Switches the current position
(define make-switch
(lambda ()
(let ((state (vector #f))) ; All switches are off when manufactured.
(lambda (message)
(cond ((eq? message ':show-position)
(if (vector-ref state 0) 'on 'off))
((eq? message ':toggle!)
(vector-set! state 0 (not (vector-ref state 0))))
(else (error "switch: unrecognized message")))))))
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)
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.
Note that you are creating an 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 will want to make a two-element vector. Element 0 of that vector will be the value of the tally. Element 1 of that vector will be the count of operations.
b. Test your procedure.
Wednesday, 6 December 2001 [Samuel A. Rebelsky]
http://www.math.grin.edu/~stone/courses/scheme/object-oriented-programming.xhtml.
2000F/Labs/oop.html.
Tuesday, 1 May 2001 [Samuel A. Rebelsky]
2001S/Labs/oop.html.
Wednesday, 4 December 2002 [Samuel A. Rebelsky]
2002F/Labs/oop.html.
Monday, 5 May 2003 [Samuel A. rebelsky]
make-switch.
2003S/Labs/oop.html.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
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 Tue Dec 9 13:59:14 2003.
The source to the document was last modified on Mon Sep 1 13:27:24 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/objects.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby