CS 119 Lab 8 – Object-Oriented Programming

Objectives

  • Introduce objects with local state which may change over time
  • 1. Evaluate the file lab8.scm. Take a look at the function make-account. This function creates an object which we will refer to as an account. This object has a local state which is the balance. The initial value of the balance is entered as an argument to make-account. Notice that this object is actually the procedure dispatch.

    Try defining two objects with make-account:
            (define acct1 (make-account 100))
            (define acct2 (make-account 200))

    What happens when we ask scheme for the value of acct1?
    What happens when we enter (acct1 ‘withdraw)? Why did this happen?

    Try the following:
            ((acct1 ‘withdraw) 50)
            ((acct1 ‘deposit) 40)
            ((acct2 ‘withdraw) 10)

    2. In software-testing applications it is useful to be able to count the number of times a given function f is called during the course of a computation. We may do so by creating an object which has a local state which is a counter. If the input to our object is the special symbol ‘how-many-calls?, then it returns the value of the counter. If the input is the special symbol ‘reset-count, then the counter is reset to zero. For any other input, the object returns the result of calling f on that input and increments the counter.

    For example, we could make a monitored version of the sqrt function:

    Þ (define s (make-monitored sqrt))
    Þ (s 100)
    10
    Þ (s ‘how-many-calls?)
    1

    Assignment:
    Write a function (make-monitored f) which performs as we have described. Test it by creating a monitored sqrt as in the example.

    3. Email your files containing the assignments to jzimmerm@goucher.edu.