Lecture 20: Objects I invented the term ObjectOriented, and I can tell you I did not have C++ in mind. — Alan Kay CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Menu • A better counter • PS8 Demo (Spencer) • Programming with Objects 13 March 2003 CS 200 Spring 2003 2 nextx from Lecture 16 global (define x 0) environment (define (nextx) + : #<primitive:+> (set! x (+ x 1)) nextx: x : 24 x) > (nextx) 1 parameters: > (set! x 23) body: (begin (set! x (+ x 1)) x) > (next x) 24 13 March 2003 CS 200 Spring 2003 3 A Better Counter • The place that keeps track of the count would be part of the counter, not part of the global environment • Can we do this? 13 March 2003 CS 200 Spring 2003 4 Recall from Lecture 17: Application 1. Construct a new frame, enclosed in the environment of this procedure 2. Make places in that frame with the names of each parameter 3. Put the values of the parameters in those places 4. Evaluate the body in the new environment 13 March 2003 CS 200 Spring 2003 5 A Better Counter (define (make-counter) ((lambda (count) (lambda () (set! count (+ 1 count)) count)) 0)) 13 March 2003 CS 200 Spring 2003 6 (define (make-counter) ((lambda (count) (lambda () (set! count (+ 1 count)) count)) 0)) > (define mycount (make-counter)) > (mycount) 1 > (mycount) 2 > (mycount) 3 13 March 2003 global environment count : + : #<primitive:+> make-counter: mycount: 3 1 0 2 parameters: body: ((lambda … parameters: body: (lambda () (set! count …) CS 200 Spring 2003 7 An Even Better Counter (define (make-ocounter) ((lambda (count) (lambda (message) (if (eq? message 'reset) (set! count 0) (if (eq? message 'next) (set! count (+ 1 count)) (if (eq? message 'how-many) count))))) 0)) 13 March 2003 CS 200 Spring 2003 8 Using Counter > > > > > 3 > > 0 (define bcounter (make-ocounter)) (bcounter 'next) (bcounter 'next) (bcounter 'next) (bcounter 'how-many) (bcounter 'reset) (bcounter 'how-many) 13 March 2003 CS 200 Spring 2003 9 Objects • When we package state and procedures together we have an object • Programming with objects is object-oriented programming 13 March 2003 CS 200 Spring 2003 10 PS8 Demo Charlottesville Music Database CS200 Spring 2002 Spencer Stockdale and Russ O’Reagan http://www.people.virginia.edu/~sas9t/csfinal/index.php3 13 March 2003 CS 200 Spring 2003 11 Why Your PS8 Sites Should be 2.357 Times Better • Last year’s students got 15 days for PS8, you get 25 days! • Their team was 2 students, your teams can be 1-6 students (preferably 3-4) – Start thinking about projects and talking to potential teammates now, so you can request your team by March 31 > (* (/ 25 15) (sqrt (/ 4 2))) 2.3570226039551585 13 March 2003 CS 200 Spring 2003 Expectations scale as sqrt of number of people involved, because of management overhead. 12 Why Your PS8 Sites Should be 3.877 Times Better • According to Tyson, you are 4.1% smarter and better prepared than last year’s students! • According to Moore, you have 58% more computing power than last year’s students! > (* (/ 25 15) (sqrt (/ 4 2)) 1.041 1.58) 3.876783638533366 13 March 2003 CS 200 Spring 2003 13 Why Your PS8 Sites Should be 8 Million Times Better! • This year’s guest speaker’s web site gets 213M unique visitors per month; last year’s guest speaker’s web site gets maybe 100. > (* (/ 25 15) (sqrt (/ 4 2)) 1.041 1.58 (/ 213000000 100)) 8257549.150076069 Note: my expectations are very reasonable. I will settle for PS8 projects that are only 1M times better than Spencer’s. 13 March 2003 CS 200 Spring 2003 14 Object-Oriented Programming 13 March 2003 CS 200 Spring 2003 15 Who was the first object-oriented programmer? 13 March 2003 CS 200 Spring 2003 16 By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent. Ada, Countess of Lovelace, around 1830 13 March 2003 CS 200 Spring 2003 17 Simula • Considered the first “object-oriented” programming language • Language designed for simulation by Kristen Nygaard and Ole-Johan Dahl (Norway, 1962) • Had special syntax for defining classes that packages state and procedures together 13 March 2003 CS 200 Spring 2003 18 Counter in Simula class counter; integer count; begin procedure reset(); count := 0; end; procedure next(); count := count + 1; end; integer procedure how-many(); how-many := count; end; end 13 March 2003 CS 200 Spring 2003 19 XEROX Palo Alto Research Center (PARC) 1970s: • Bitmapped display • Graphical User Interface – Steve Jobs paid $1M to visit and PARC, and returned to make Apple Lisa/Mac) • • • • Ethernet First personal computer (Alto) PostScript Printers Object-Oriented Programming 13 March 2003 CS 200 Spring 2003 20 Dynabook, 1972 (Just a model) 13 March 2003 The best way to predict the future is to invent it. Alan Kay CS 200 Spring 2003 21 Dynabook • Tablet computer • Intented as tool for learning • Kay wanted children to be able to program it also • Hallway argument, Kay claims you could define “the most powerful language in the world in a page of code” • Proof: Smalltalk – Scheme is as powerful, but takes two pages 13 March 2003 CS 200 Spring 2003 22 BYTE Magazine, August 1981 13 March 2003 CS 200 Spring 2003 23 Smalltalk • Everything is an object • Objects communicate by sending and receiving messages • Objects have their own state (which may contain other objects) • How do you do 3 + 4? send the object 3 the message “+ 4” 13 March 2003 CS 200 Spring 2003 24 Counter in Smalltalk class name counter instance variable names count new count <- 0 next count <- count + 1 how-many ^ count 13 March 2003 CS 200 Spring 2003 25 Counter in Scheme (define (make-ocounter) ((lambda (count) (lambda (message) (if (eq? message 'reset) (set! count 0) (if (eq? message 'next) (set! count (+ 1 count)) (if (eq? message 'how-many) count))))) 0)) 13 March 2003 CS 200 Spring 2003 26 Counter in Scheme using let (define (make-ocounter) (let ((count 0)) (lambda (message) (if (eq? message 'reset) (set! count 0) (if (eq? message 'next) (set! count (+ 1 count)) (if (eq? message 'how-many) count)))))) 13 March 2003 CS 200 Spring 2003 27 Defining ask (ask Object Method) > (ask bcounter 'how-many) 0 > (ask bcounter 'next) > (ask bcounter 'how-many) 1 (define (ask object message) (object message)) 13 March 2003 CS 200 Spring 2003 28 Charge • PS6: out today – Programming with Objects • Monday: Subtyping 13 March 2003 CS 200 Spring 2003 29