Docsity

Programming Objects - Lecture Slides | CS 1120, Study notes of Computer Science

Toggle sidebar
bg1
1
David Evans
http://www.cs.virginia.edu/evans
CS150: Computer Science
University of Virginia
Computer Science
Lecture 23:
Lecture 23:
Programming
Programming
with Objects
with Objects
2
Lecture 23: Programming with Objects
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
3
Lecture 23: Programming with Objects
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
4
Lecture 23: Programming with Objects
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
5
Lecture 23: Programming with Objects
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
6
Lecture 23: Programming with Objects
Defining 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))
pf3
pf4

Partial preview of the text

Download Programming Objects - Lecture Slides | CS 1120 and more Study notes Computer Science in PDF only on Docsity!

David Evans http://www.cs.virginia.edu/evans

CS150: Computer Science University of Virginia Computer Science

Lecture 23: Lecture 23:

Programming Programming

with Objects with Objects

Lecture 23: Programming with Objects 2

Reminder

• Start thinking of ideas of PS9 and

discussing them on the forum

http://www.cs.virginia.edu/forums/viewforum.php?f=

– You can also vote in the “should we have a

quiz Monday” poll

http://www.cs.virginia.edu/forums/viewtopic.php?t=

http://www.sportsline.com/collegebasketball/scoreboard

Lecture 23: Programming with Objects 3

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

Lecture 23: Programming with Objects 4

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

Lecture 23: Programming with Objects 5

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

Lecture 23: Programming with Objects 6

Defining ask

> (define bcounter (make-counter))

> (ask bcounter 'current)

> (ask bcounter 'next)

> (ask bcounter 'current)

(ask Object Method)

(define (ask object message)

(object message))

Lecture 23: Programming with Objects 7

Inheritance

Lecture 23: Programming with Objects 8

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

Lecture 23: Programming with Objects 9

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). Lecture 23: Programming with Objects 10

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.)

Lecture 23: Programming with Objects 11

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))))))

Lecture 23: Programming with Objects 12

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)))))

Lecture 23: Programming with Objects 19

PS

Make an adventure game

programming with objects

Many objects in our game have

similar properties and behaviors,

so we use inheritance.

Lecture 23: Programming with Objects 20

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.

Lecture 23: Programming with Objects 21

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.

Lecture 23: Programming with Objects 22

Are there class hierarchies

like this in the “real world”

or just in fictional worlds

like Charlottansville?

Lecture 23: Programming with Objects 23

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