lecture23

Information about lecture23

Published on August 8, 2007

Author: Wanderer

Source: authorstream.com

Content

Slide1:  David Evans http://www.cs.virginia.edu/evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects Reminder:  Reminder Start thinking of ideas of PS9 and discussing them on the forum http://www.cs.virginia.edu/forums/viewforum.php?f=28 You can also vote in the 'should we have a quiz Monday' poll http://www.cs.virginia.edu/forums/viewtopic.php?t=1651 http://www.sportsline.com/collegebasketball/scoreboard Problem-Solving Strategies:  Problem-Solving Strategies PS1-PS4: Functional Programming Focused on procedures Break a problem into procedures that can be combined to solve it PS5: Imperative Programming Focused on data Design data for representing a problem and procedures for updating that data Problem-Solving Strategies:  Problem-Solving Strategies PS6: 'Object-Oriented Programming' Focused on objects: package procedures and state Model a problem by dividing it into objects Lots of problems in real (and imaginary) worlds can be thought of this way Counter Object:  Counter Object (define (make-counter) (let ((count 0)) (lambda (message) (cond ((eq? message ’reset!) (set! count 0)) ((eq? message ’next!) (set! count (+ 1 count))) ((eq? message ’current) count) (else (error 'Unrecognized message')))))) Instance variable Methods Defining ask:  Defining ask andgt; (define bcounter (make-counter)) andgt; (ask bcounter 'current) 0 andgt; (ask bcounter 'next) andgt; (ask bcounter 'current) 1 (ask Object Method) (define (ask object message) (object message)) Inheritance:  Inheritance There are many kinds of numbers…:  There are many kinds of numbers… Whole Numbers (0, 1, 2, …) Integers (-23, 73, 0, …) Fractions (1/2, 7/8, …) Floating Point (2.3, 0.0004, 3.14159) But they can’t all do the same things We can get the denominator of a fraction, but not of an integer make-fraction:  make-fraction (define make-fraction (lambda (numerator denominator) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numerator denominator)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value))) ((eq? message ‘get-numerator) (lambda (self) numerator)) ((eq? message ‘get-denominator) (lambda (self) denominator)) ))))) Same as in make-number Note: our add method evaluates to a number, not a fraction object (which would be better). Why is redefining add a bad thing?:  Why is redefining add a bad thing? Cut-and-paste is easy but… There could be lots of number methods (subtract, multiply, print, etc.) Making the code bigger makes it harder to understand If we fix a problem in the number add method, we have to remember to fix the copy in make-fraction also (and real, complex, float, etc.) make-fraction:  make-fraction (define (make-fraction numer denom) (let ((super (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else (super message)))))) Making Subobjects:  Making Subobjects (define (make-fraction numer denom) (make-subobject (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else #f))))) Implementing make-subobject:  Implementing make-subobject (define (make-subobject super imp) (lambda (message) (if (eq? message ’super) (lambda (self) super) (let ((method (imp message))) (if method method (super message)))))) Using Fractions:  Using Fractions andgt; (define half (make-fraction 1 2)) andgt; (ask half 'value) 1/2 andgt; (ask half 'get-denominator) 2 andgt; (ask half 'add (make-number 1)) 3/2 andgt; (ask half 'add half) 1 Slide15:  andgt; (trace ask) andgt; (trace eq?) andgt; (ask half 'add half) |(ask #andlt;procedureandgt; add #andlt;procedureandgt;) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask #andlt;procedureandgt; value) | |(eq? value value) | |#t | 1/2 | (ask #andlt;procedureandgt; value) | |(eq? value value) | |#t | 1/2 |1 1 Slide16:  andgt; (trace ask) andgt; (trace eq?) andgt; (ask half 'add half) |(ask #andlt;procedureandgt; add #andlt;procedureandgt;) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask #andlt;procedureandgt; value) | |(eq? value value) | |#t | 1/2 | (ask #andlt;procedureandgt; value) | |(eq? value value) | |#t | 1/2 |1 1 make-number make-fraction Inheritance:  Inheritance Inheritance is using the definition of one class to make another class make-fraction uses make-number to inherit the behaviors of number Speaking about Inheritance:  Speaking about Inheritance Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. Number Fraction PS6Make an adventure game programming with objectsMany objects in our game have similar properties and behaviors, so we use inheritance.:  PS6 Make an adventure game programming with objects Many objects in our game have similar properties and behaviors, so we use inheritance. PS6 Classes:  PS6 Classes sim-object physical-object place mobile-object thing person student police-officer make-class is the procedure for constructing objects in the class class student inherits from person which inherits from mobile-object which inherits from physical-object which inherits from sim-object. PS6 Objects:  PS6 Objects object physical-object place mobile-object thing person student police-officer Cabal Hall Recursa Alyssa P. Hacker (make-place name) evaluates to an object that is an instance of the class place. Are there class hierarchies like this in the “real world” or just in fictional worlds like Charlottansville?:  Are there class hierarchies like this in the 'real world' or just in fictional worlds like Charlottansville? Charge:  Charge Monday: Quiz on GEB reading (depending on poll) History of Object-Oriented Programming PS6 due Friday Start thinking about PS9 project ideas Use the forum to find teammates and propose ideas

Related presentations


Other presentations created by Wanderer

RCM2 Ganesan
17. 08. 2007
0 views

RCM2 Ganesan

System Architect
21. 09. 2007
0 views

System Architect

Customer Retention
28. 09. 2007
0 views

Customer Retention

intrusion detection monitoring
07. 10. 2007
0 views

intrusion detection monitoring

baptista
10. 10. 2007
0 views

baptista

YoungEntrepreneurs China
11. 10. 2007
0 views

YoungEntrepreneurs China

presentation proposed programme
12. 10. 2007
0 views

presentation proposed programme

ch19 lecture
12. 10. 2007
0 views

ch19 lecture

balla reinhart F100 1 pres
18. 10. 2007
0 views

balla reinhart F100 1 pres

Erasmus Charte Universitarie
23. 10. 2007
0 views

Erasmus Charte Universitarie

essayformatTHESIS
26. 08. 2007
0 views

essayformatTHESIS

Sharon
26. 08. 2007
0 views

Sharon

7 Panama Esp
22. 10. 2007
0 views

7 Panama Esp

preference
07. 11. 2007
0 views

preference

Go Forth
17. 08. 2007
0 views

Go Forth

sf wireless
29. 10. 2007
0 views

sf wireless

Convulsoes Neonatais e Epilepsia
28. 12. 2007
0 views

Convulsoes Neonatais e Epilepsia

ch7F07govt2302
31. 12. 2007
0 views

ch7F07govt2302

Presidential character
03. 01. 2008
0 views

Presidential character

potma
03. 01. 2008
0 views

potma

Maitland
09. 10. 2007
0 views

Maitland

overweight obesity
08. 08. 2007
0 views

overweight obesity

Minority Stress Gray APA2006
08. 08. 2007
0 views

Minority Stress Gray APA2006

MoAc0304
08. 08. 2007
0 views

MoAc0304

japanese02s gyro
26. 08. 2007
0 views

japanese02s gyro

Bjorn AFCEATTN CDRNeurath
19. 11. 2007
0 views

Bjorn AFCEATTN CDRNeurath

Rosenzweig Presentation
29. 12. 2007
0 views

Rosenzweig Presentation

martin weller lams
20. 07. 2007
0 views

martin weller lams

26221
26. 08. 2007
0 views

26221

NAATPN Presentation
11. 12. 2007
0 views

NAATPN Presentation

D Levy Transp
21. 09. 2007
0 views

D Levy Transp

NicosiaRaymondPawson
26. 08. 2007
0 views

NicosiaRaymondPawson

646family
24. 02. 2008
0 views

646family

AfricanSlaveTrades
26. 02. 2008
0 views

AfricanSlaveTrades

mms 04 13 elearning
27. 06. 2007
0 views

mms 04 13 elearning

Mathematical Moodle final
27. 06. 2007
0 views

Mathematical Moodle final

inbrieffeb07
28. 02. 2008
0 views

inbrieffeb07

LaWeyl
08. 08. 2007
0 views

LaWeyl

hurricane katrina
13. 03. 2008
0 views

hurricane katrina

meaning
27. 11. 2007
0 views

meaning

transparents Berleur
18. 03. 2008
0 views

transparents Berleur

ChinaandJapanPt2
25. 03. 2008
0 views

ChinaandJapanPt2

BroadbandServies
26. 03. 2008
0 views

BroadbandServies

sinclair prc precip
03. 10. 2007
0 views

sinclair prc precip

8 Mru Patel
07. 04. 2008
0 views

8 Mru Patel

COE 9 Jan 06
28. 03. 2008
0 views

COE 9 Jan 06

060928 Energy Challenges Thun
30. 03. 2008
0 views

060928 Energy Challenges Thun

program
27. 11. 2007
0 views

program

adam smith
09. 04. 2008
0 views

adam smith

crcagu03
10. 04. 2008
0 views

crcagu03

Susan Wachter
13. 04. 2008
0 views

Susan Wachter

MickLilley MacquarieBank
14. 04. 2008
0 views

MickLilley MacquarieBank

experiencia
19. 06. 2007
0 views

experiencia

Europa desde el Cielo 2126
19. 06. 2007
0 views

Europa desde el Cielo 2126

Estrategia empresarial
19. 06. 2007
0 views

Estrategia empresarial

Estadio Allianz Arena 2125
19. 06. 2007
0 views

Estadio Allianz Arena 2125

Esculturas hechas con Arena 2124
19. 06. 2007
0 views

Esculturas hechas con Arena 2124

sesame
26. 11. 2007
0 views

sesame

pierre danon
22. 04. 2008
0 views

pierre danon

Globos Aerostaticos 2133
19. 06. 2007
0 views

Globos Aerostaticos 2133

Gaturro oficina
19. 06. 2007
0 views

Gaturro oficina

Frases para reflexionar 2131
19. 06. 2007
0 views

Frases para reflexionar 2131

Football
19. 06. 2007
0 views

Football

twilight
26. 08. 2007
0 views

twilight

WW
04. 01. 2008
0 views

WW

EvidenceMatters
19. 06. 2007
0 views

EvidenceMatters

p6 alina
26. 08. 2007
0 views

p6 alina

Fantasia 2042
19. 06. 2007
0 views

Fantasia 2042

older adults nut
08. 08. 2007
0 views

older adults nut

mellange presentation en
27. 06. 2007
0 views

mellange presentation en

SH Presentation Sunny Hills
26. 08. 2007
0 views

SH Presentation Sunny Hills

IAFC IndiaPresentation
17. 08. 2007
0 views

IAFC IndiaPresentation

00017079
26. 08. 2007
0 views

00017079

04RandomVariables
07. 12. 2007
0 views

04RandomVariables

Lecture 24 Muhammed and Islam
17. 08. 2007
0 views

Lecture 24 Muhammed and Islam

00 norby
26. 08. 2007
0 views

00 norby

CN7 Learning2
14. 12. 2007
0 views

CN7 Learning2

Fumar Mata
19. 06. 2007
0 views

Fumar Mata

do dont show
16. 06. 2007
0 views

do dont show

Direc TV
16. 06. 2007
0 views

Direc TV

BAM CIDOC 2006 folien
16. 06. 2007
0 views

BAM CIDOC 2006 folien

Dr Anwar1
16. 06. 2007
0 views

Dr Anwar1

biouml gcb 2003
16. 11. 2007
0 views

biouml gcb 2003

PPConference 28 02 07 E Marcova
14. 03. 2008
0 views

PPConference 28 02 07 E Marcova

dominguezhills
26. 08. 2007
0 views

dominguezhills

Festival de hielo Harbin
19. 06. 2007
0 views

Festival de hielo Harbin

ePHocus update Rebecca Hills
26. 08. 2007
0 views

ePHocus update Rebecca Hills

Gerenciamiento 2132
19. 06. 2007
0 views

Gerenciamiento 2132

30 mw a si machine
26. 08. 2007
0 views

30 mw a si machine

excursions
19. 06. 2007
0 views

excursions

tl outcomes
12. 10. 2007
0 views

tl outcomes

MIC 03 Dodge WebQuest
27. 06. 2007
0 views

MIC 03 Dodge WebQuest

mms 04 16 dim
27. 06. 2007
0 views

mms 04 16 dim

PRESENTACION PROGRESO
22. 10. 2007
0 views

PRESENTACION PROGRESO

Errores irreparables 1988
19. 06. 2007
0 views

Errores irreparables 1988

EDLafcoPresent092805 chew
26. 08. 2007
0 views

EDLafcoPresent092805 chew

AH summer 07
26. 08. 2007
0 views

AH summer 07

1 Intro class1
26. 08. 2007
0 views

1 Intro class1

Murrieta PKS
08. 08. 2007
0 views

Murrieta PKS