Lecture java1: Object-Oriented Programming is About Data



  1. "Java, the fashionable" VS "Java, what is it really"

  2. The title object-oriented programming is a misnomer. It's about
    object-oriented computation        ---        class-based programming
    

    The first design goal is to lay-out the various classes of data that a program processes and how these forms of data interrelate.

    Sounds familiar? This is all the work we go through before we get to do the "real" stuff: programming.

    So what form of data does Java offer? We know from the first few weeks that we want:

    Let's see how these things work out in the Java world.

  3. basic forms of data:

    numbers (5, 7.45; no fractions, no complex): 5 + 55
    int, double

    Booleans (true, false): if true ...
    boolean

    Integer objects new Integer(5): new Integer(5).plus(55)
    Integer

    Boolean objects new Boolean(true)
    Integer

  4. compound data: A monkey has four legs and needs 55 cubic-meters of living space.

    In the Scheme world:

    (define-struct monkey (legs space))
    ;; A monkey is a structure: (make-monkey number number). 
    
    ;; Creating a monkey: 
    (make-monkey 4 55)
    

    In Java:

     
    class Monkey {
      int legs; 
      int space:
      Monkey(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
    }
    
    // Creating a Monkey: 
    new Monkey(4,55)
    


  5. mixed data: An animal is either a monkey, an elephant, or a spider.

    In the Scheme world:

    (define-struct spider (legs space))
    ;; A spider is a structure: (make-spider 8 .2)
    
    (define-struct elephant (legs space))
    ;; An elephant is a structure: (make-elephant 8 200)
    
    ;; An animal is either 
    ;;  - a monkey 
    ;;  - an elephant 
    ;;  - a spider. 
    

    In the Java world:

    interface Animal {}
    
    class Spider implements Animal {
      int legs; 
      int space; 
      Spider(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
    }
    
    class Elephant implements Animal {
      int legs; 
      int space; 
      Elephant(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
    }
    
    class Monkey implements Animal {
      int legs; 
      int space; 
      Monkey(int _legs, int _space) {
        legs = _legs; 
        space = _space; 
      }
    }
    


  6. arbitrarily large data: a list of integers, a family tree, etc.

    In the Scheme world:

    (define-struct child (name father mother))
    
    ;; An family tree is either 
    ;;  - empty
    ;;  - or a structure: (make-child symbol FT1 FT2)
    ;;    where FT1 and FT2 are family trees. 
    

    In the Java world:

    interface FamilyTree {}
    
    class Empty implements FamilyTree {}
    
    class Child implements FamilyTree {
      String name; 
      FamilyTree father; 
      FamilyTree mother; 
      Child(String name, FamilyTree _father, FamilyTree _mother) {
        name   = _name; 
        father = _father; 
        mother = _mother; 
      }
    }
    
    // Creating Family Trees: 
    
    new Empty()
    
    new FamilyTree("Ben", new Empty(), new Empty())
    


  7. So, we have layed out our data, now we are ready to design the methods. Yes, not programs -- methods.




Matthias Felleisen This page was generated on Fri Feb 26 14:50:00 CST 1999.