Laboratory Exercises For Computer Science 153

Object-Oriented Design

Object-Oriented Design

Now that we have seen some implementation of object oriented programming, it is time to develop our skills at solving problems from an object-oriented approach. To do this well, we need to disregard what we know about functional problem solving and look at problems from a completely different perspective. We need to identify the objects and classes of objects within a problem, and we need to identify the relationships among those objects and classes.

Goals: This lab will stress the importance of using relationships among independently operating objects to solve large problems. Upon completion, the student should be able to create and edit a list of classes that pertain to a problem and how to relate those classes to one another using data-flow diagrams. The student should also have a general familiarity with the idea of delegation.

Example of an Object:

The registrar's office of a college wishes to implement a database to keep track of its students. The office is concerned with the following pieces of information for each student: graduation year, number of credits earned in to date, courses currently enrolled in, and major and advisor, if the student has declared her major.

Designing a Student Class:

As part of this problem, we let each student, represented by an object, keeps track of things herself. Here we wish to create separate entities of the type student. Each entity should operate independently of the others.

To use a group of similar entities, in this case students, we define a class of them. The class definition will allow us to create new students, each a separate entity with the proper structure. As we identify additional students, we always need to be able to create or "instantiate" new student objects.

The following class definition is set up so that a student can tally credit for completed courses and can declare the Math or Computer Science major with one of the available Math or CS advisors. Each object will accept the messages completed, show_credits, show_major, declare, and show_advisor with appropriate arguments, and will take the appropriate action.

(define student-class
  (lambda (new-name new-major new-advisor new-credits)
    (let* ((name new-name)
           (major new-major)
           (advisor new-advisor)
           (credits new-credits))
       (lambda (message . arguments)
         (case message
           ((completed)    ;; update credits with additional completed
               (cond ((null? arguments) 
                        (error 'completed "need to specify new credits"))
                     ((> (length arguments) 1)
                        (error 'completed "only credits may be specified"))
                     ((or (not (number? (car arguments)))
                          (not (positive? (car arguments))))
                        (error 'completed "only positive numbers for credits"))
                     (else (set! credits (+ credits (car arguments))))))
           ((declare) ;; change both major and advisor
                  (if (= (length arguments) 2)
                      (begin
                         (set! advisor (car arguments))
                         (set! major (cadr arguments)))
                       (error 'change_major "specify both advisor and major")))
           ((show_credits) ;; return current number of credits earned
                  credits)
           ((show_major)   ;; return name of current major
                  major)
           ((show_advisor) ;; return name of current advisor
                   advisor)
         )
       )
    )
  )
)
  1. Copy the above code into Scheme, and use it to define some objects with the commands:

    (define dick (student-class "Dick" "undeclared" "staff" 0))
    (define jane (student-class "Jane" "undeclared" "staff" 0))
    (define rick (student-class "Dick" "undeclared" "staff" 0))
    (define mary jane)
    
    This sequence of definitions will create separate objects for Dick and Jane as undeclared majors with unspecified advisors and zero credits. It will also define Rick as an object masquerading as Dick, and make Mary an alias that Jane uses.

  2. Try the following commands and describe what they do to the objects, in terms of output to the screen and what happens internally to the object.

    (jane 'declare "Walker" "CS")
    (dick 'completed 4)
    
  3. Check that the appropriate objects made the appropriate changes with the commands:

    (jane 'show_major)
    (dick 'show_major) 
    (rick 'show_major) 
    (mary 'show_major) 
    
    (jane 'show_advisor)
    (dick 'show_advisor)
    (rick 'show_advisor)
    (mary 'show_advisor)
    
    (jane 'show_credits)
    (dick 'show_credits)
    (rick 'show_credits)
    (mary 'show_credits)
    

    Note that Jane still has no credits and Dick is undeclared. Why is this? What are the states of Rick and Mary?

  4. Now have Rick declare his major and let Mary complete a 4 credit course:

    (rick 'declare "Jepsen" "Math")
    (mary 'completed 4)
    

    Who are Dick's and Rick's advisors now? What about their majors? How many credits do Mary and Jane have? Explain each result.

    Why do Dick and Rick appear different now, when they were defined with identical states? If not state, then what is the factor that distinguishes one object from another?

  5. Add a command print to the student class. The result of the print command should be to display the name, advisor, major, and credits for the students in a nice format. Test this command appropriately.

  6. While each object contains information about the student's name, major, advisor, and credits, none of this information can be accessed or changed directly. Rather operations must be done through the various commands, called methods. Explain why this information cannot be referenced only through the various methods.

  7. With the methods defined, can a student change her advisor without also reentering a major? Why or why not?

  8. Add a change_major method to this class, which allows students to change from any major to any other directly, except that "Mathematics" majors can change only to "CS".


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/151.sp99/lab-oop-design.html

parts of this lab were created April 17, 1998 by Scott G. Barkley
lab in this form created April 16, 1999 by Henry M. Walker
last revised April 16, 1999