Lecture java2: A Method for Object-Oriented Programming



  1. "Java, the fashionable" VS "Java, what is it really"
  2. Emphasis in last lecture: defining data using classes and interfaces

  3. for flat data, things are pretty much like conventional arithmetic:
    Scheme Java
    (+ x 5) x + 5
    (+ x (* 5 y)) x + 5 * y
    (not w) !(w)
    (and w v) w && v
    (or w v) w || v

    So what's the meaning of:

                           w && u || v
    
    Parentheses have advantages. In particular, it is always clear which operator goes first.
  4. Now when we get to compound data, we need a new idea: methods.

    A monkey has N legs and needs M cubic meter of space. Does the monkey fit in a cage with K cubic meter of space?

    Scheme:

    (define-struct monkey (legs space))
    ;; A monkey is (make-monkey 4 55)
    
    ;; fits: monkey num -> boolean
    (define (fits a-monkey cage-space)
      ... (monkey-legs a-monkey) ...
      ... (monkey-space a-monkey) ...
      )
    
    ;; fits: monkey -> boolean
    (define (fits a-monkey cage-space)
      (<= (monkey-space a-monkey) cage-space))
    

    Java: we no longer have programs floating around, we have methods that are defined within a class. The data is implied.

    class Monkey {
      int legs; 
      int space:
      Monkey(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
    
      // ----------------------------
    
      boolean fits(int cage_space) {
       return this.space < cage_space; }
    }
    
    // Creating a Monkey: 
    new Monkey(4,55)
    // This object "knows" about a fit method
    
    // Using the fit method: OBJECT-ORIENTED COMPUTATION
      new Monkey(4,55).fits(155) 
    = return new Money(4,55).space < 155
    = return 55 < 155
    

    Exercise: Define a class Cage. A cage has N cubic meter and is available (or not). Then define a method fit that consumes a cage, not just a number.

  5. Mixed data:

    Mixed data in the Java world:

    interface Animal {
      boolean fits(int cage_space);
    }
    
    class Spider implements Animal {
      int legs; 
      int space; 
      Spider(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
      // ---------------------------
    
      
      public boolean fits(int cage_space) {
       return this.space < cage_space; }
      
    }
    
    class Elephant implements Animal {
      int legs; 
      int space; 
      Elephant(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
      // ---------------------------
    
      
      public boolean fits(int cage_space) {
       return this.space < cage_space; }
      
    }
    
    class Monkey implements Animal {
      int legs; 
      int space; 
      Monkey(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
      // ---------------------------
    
      
      public boolean fits(int cage_space) {
       return this.space < cage_space; }
      
    }
    

    Recall that Scheme needed one other concept here: the conditional. Here is the program

    ;; fits : animal num -> boolean
    (define (fits an-animal cage-space)
      (cond
        [(spider? an-animal) (< (spider-space an-animal) cage-space)]
        [(monkey? an-animal) (< (monkey-space an-animal) cage-space)]    
        [(elephant? an-animal) (< (elephant-space an-animal) cage-space)]))
    

    So how come this animal program works and how does it work?

    Animal a = new Monkey(3,55);
    
      a.fits(155)
    = new Monkey(3,55).fits(155)
    = 55 < 155
    
    The dispatch is computed by the "little man". It is built into our rules of computing. So object-oriented computing provides one thing that people realized was done again and again by programmers: dispatching.

    How about abstraction? All three definitions for fit are identical. Shouldn't we abstract? Yes -- we can get to this next time.





Matthias Felleisen This page was generated on Fri Mar 19 17:10:05 CST 1999.