CS 2135 (A01) Homework 7: Assignment and Objects

Due: October 16 (Tuesday) at 11:59pm via turnin (assignment name hwk7).

Assignment Goals


The Assignment

  1. Consider this (proposed) code for a square class, and the interactions with it that follow:

    (define make-square1
      (local [(define sidelen #f)]
        (lambda (initlen)
          (begin
            (set! sidelen initlen)
            (local [(define (area) (* sidelen sidelen))
                    (define (resize newlen) (set! sidelen newlen))]
              (lambda (service)
                (cond [(symbol=? service 'area) area]
                      [(symbol=? service 'resize) resize])))))))
    
    > (define s1 (make-square1 5))
    > ((s1 'area))
    25
    > (define s2 (make-square1 3))
    > ((s2 'area))
    9
    > ((s1 'area))
    9
    

    What's wrong with this code? Explain your answer in terms of how DrScheme evaluates programs (you do not have to show the sequence of steps in the evaluation).

  2. Correct the code from the previous question so that it produces the following interactions. Explain why your new code corrects the problems from the old code.

    > (define s1 (make-square1 5))
    > ((s1 'area))
    25
    > (define s2 (make-square1 3))
    > ((s2 'area))
    9
    > ((s1 'area))
    25
    
  3. In the following program, is the use of set! justified? Either explain why it is, or rewrite the code to get rid of the unwarranted set!(s).

    (define make-square2
      (lambda (initside topleftx toplefty)
        (local [(define topleft (make-posn topleftx toplefty))
                (define blinktimes 10)
                ;; blinkshape blinks the square on the screen 10 times
                (define blinksquare
                  (lambda ()
                     (cond [(= blinktimes 0) (set! blinktimes 10)]
                           [else (begin
                                   (draw-solid-rect topleft initside initside)
                                   (sleep .25)
                                   (clear-solid-rect topleft initside initside)
                                   (set! blinktimes (- blinktimes 1))
                                   (blinksquare))])))]
          (lambda (service)
            (cond [(symbol=? service 'blink) blinksquare])))))
    
  4. In the following program, is the use of set! justified? Either explain why it is, or rewrite the code to get rid of the unwarranted set!(s).

    (define make-square3
      (lambda (initlen initx inity)
        (local [(define topleft (make-posn initx inity))]
          (lambda (service)
            (cond [(symbol=? service 'get-corner) topleft]
    	      [(symbol=? service 'draw)
    	       (lambda ()
    		 (draw-solid-rect topleft initlen initlen)))
    	      [(symbol=? service 'move)
    	       (lambda (newx newy)
    		 (set! topleft (make-posn newx newy)))]
                  )))))
    

What to Turn In

Turn in a single file hwk7.ss (or hwk7.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Hints and Guidelines


Back to the Assignments page