6.001 Recitation – Oct 16, 2002

 

RI: Konrad Tollmar

 

Basic Object System

 

 

Object-oriented terminology

 

Class:       

Specifies the common behavior of entities. First approximation in scheme, a "maker" procedure.

 

 

 

Instance:

A particular object or entity of a given class. In scheme, an instance is a message-handling procedure made by the maker procedure.

 

 

 

 

 

1. A Car maker

Write a HOP car-maker that create a message-handling procedure

, and maintain the states type and running. You should be able to get these states through the message type? and run? And you should be able to change the state running through the messages start and stop.

 

Class is a maker procedure that generates an instance

                      (define make-car (local-vars)

                 (define private-procedures … )

                 (lambda (message) … ))

Instance is a procedure that returns an appropriate method based on the message

                      (define s (make-car))

         s ==>

 

 


 


 

 

 

(define (make-car type running)

  (define (start)

    (cond ((equal? running #f) (set! running #t) #t)

             (else (error "car is already running"))))

  (define (stop)

    (cond ((equal? running #t) (set! running #f) #t)

             (else (error "car is not running"))))

  (lambda (msg) 

    (cond ((eq? msg 'TYPE) type)

             ((eq? msg 'RUNNING) running)

             ((eq? msg 'STOP) (stop))

             ((eq? msg 'START) (start))

             (else (error "car can't" msg)))))

 

 

 

 

2. A Painter

 

Specify a Painter class that could set-up a paint area, and draw on that area using different colors. Use a Class diagram to specify the class, implement the class, and show with some instance diagrams how to create instances from this class.

 

(define myFirstPainting

  (make-painting 100 ’WHITE))

(myFirstPainting ’COLOR ’RED)

 

(myFirstPainting ’PAINT ’(100 100))

 

 

 

(define (make-painting size color)
(define (set-color new-color)
  (set! color new-color)
(define (paint x y)
  (…)
(lambda (msg . args)
  (cond ((eq? msg ’SET-COLOR) (set-color (car args))
        ((eq? msg ’PAINT) (paint … )
        (else (error "car can't" msg)))))

 

 

 

3. A drawing program

 

Specify with an object-oriented model a drawing program. Use a class diagram to specify needed classes, and show how the classes relate to each other.