Unformatted text preview:

1David Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 23: Lecture 23: Programming Programming with Objectswith Objects2Lecture 23: Programming with ObjectsReminder• Start thinking of ideas of PS9 and discussing them on the forumhttp://www.cs.virginia.edu/forums/viewforum.php?f=28– You can also vote in the “should we have a quiz Monday” pollhttp://www.cs.virginia.edu/forums/viewtopic.php?t=1651http://www.sportsline.com/collegebasketball/scoreboard3Lecture 23: Programming with ObjectsProblem-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 data4Lecture 23: Programming with ObjectsProblem-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 way5Lecture 23: Programming with ObjectsCounter 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 variableMethods6Lecture 23: Programming with ObjectsDefining ask> (define bcounter (make-counter))> (ask bcounter 'current)0> (ask bcounter 'next)> (ask bcounter 'current)1(ask Object Method)(define (ask object message)(object message))27Lecture 23: Programming with ObjectsInheritance8Lecture 23: Programming with ObjectsThere 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 integer9Lecture 23: Programming with Objectsmake-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 inmake-numberNote: our addmethod evaluatesto a number, nota fraction object (which would be better).10Lecture 23: Programming with ObjectsWhy 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.)11Lecture 23: Programming with Objectsmake-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))))))12Lecture 23: Programming with ObjectsMaking 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)))))313Lecture 23: Programming with ObjectsImplementing make-subobject(define (make-subobject super imp)(lambda (message)(if (eq? message ’super)(lambda (self) super)(let ((method (imp message)))(if methodmethod(super message))))))14Lecture 23: Programming with ObjectsUsing Fractions> (define half (make-fraction 1 2))> (ask half 'value)1/2> (ask half 'get-denominator)2> (ask half 'add (make-number 1))3/2> (ask half 'add half)115Lecture 23: Programming with Objects> (trace ask)> (trace eq?)> (ask half 'add half)|(ask #<procedure> add #<procedure>)| (eq? add value)| #f| (eq? add get-denominator)| #f| (eq? add get-numerator)| #f| (eq? add value)| #f| (eq? add add)| #t| (ask #<procedure> value)| |(eq? value value)| |#t| 1/2| (ask #<procedure> value)| |(eq? value value)| |#t| 1/2|1116Lecture 23: Programming with Objects> (trace ask)> (trace eq?)> (ask half 'add half)|(ask #<procedure> add #<procedure>)| (eq? add value)| #f| (eq? add get-denominator)| #f| (eq? add get-numerator)| #f| (eq? add value)| #f| (eq? add add)| #t| (ask #<procedure> value)| |(eq? value value)| |#t| 1/2| (ask #<procedure> value)| |(eq? value value)| |#t| 1/2|11make-numbermake-fraction17Lecture 23: Programming with ObjectsInheritanceInheritance is using the definition of one class to make another classmake-fraction uses make-number to inheritthe behaviors of number18Lecture 23: Programming with ObjectsSpeaking about InheritanceFraction inheritsfrom Number.Fraction is a subclassof Number.The superclassof Fraction is Number.NumberFraction419Lecture 23: Programming with ObjectsPS6Make an adventure game programming with objectsMany objects in our game have similar properties and behaviors, so we use inheritance.20Lecture 23: Programming with ObjectsPS6 Classessim-objectphysical-objectplacemobile-objectthingpersonstudentpolice-officermake-classis theprocedure for constructingobjects in the class classstudent inherits from personwhich inherits from mobile-objectwhich inherits from physical-objectwhich inherits from sim-object.21Lecture 23: Programming with ObjectsPS6 Objectsobjectphysical-objectplacemobile-objectthingpersonstudentpolice-officerCabal HallRecursaAlyssa P. Hacker(make-place name)evaluates to an objectthat is an instance ofthe class place.22Lecture 23: Programming with ObjectsAre there class hierarchies like this in the “real world”or just in fictional worlds like Charlottansville?23Lecture 23: Programming with ObjectsCharge• 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


View Full Document

UVA CS 1120 - Programming with Objects

Download Programming with Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming with Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming with Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?