object-oriented programming. object-oriented programming first goal: define and describe the...

27
Object-Oriented Programming

Upload: clyde-ball

Post on 31-Dec-2015

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Object-Oriented Programming

Page 2: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Object-oriented programming First goal: Define and describe the objects of

the world Noun-oriented Focus on the domain of the program The object-oriented analyst asks herself: “The program I’m

trying to write relates to the real world in some way. What are the things in the real world that this program relates to?”

Example: Imagine you’re building an O-O Banner What are the objects? Students, transcripts, classes, catalog, major-requirements,

grades, rooms…

Page 3: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Objects Objects as models of real world entities Objects as Cells

Independent, indivisible, interacting—in standard ways Scales well

Complexity: Distributed responsibility Robustness: Independent Supporting growth: Same mechanism everywhere Reuse: Provide services, just like in real world

Page 4: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Defining an object Objects know things.

Data that is internal to the object. We often call those instance variables.

Objects can do things. Behavior that is internal to the object. We call functions that are specific to an object methods.

But you knew that one already.

We access both of these using dot notation object.variable object.method()

Page 5: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Classes Objects are instances of classes in many

object-oriented languages. Including Smalltalk, Java, JavaScript, and Python.

A class defines the data and behavior of an object. A class defines what all instances of that class

know and can do.

Page 6: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Example to motivate objects: SlideShow Let’s build a program to show a slide show.

It shows a picture. Then plays a corresponding sound.

We’ll use the introduced-but-never-used blockingPlay() to make the execution wait until the sound is done.

Page 7: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Slideshow def playslideshow(): pic = makePicture(getMediaPath("barbara.jpg")) snd = makeSound(getMediaPath("bassoon-c4.wav")) show(pic) blockingPlay(snd) pic = makePicture(getMediaPath("beach.jpg")) snd = makeSound(getMediaPath("bassoon-e4.wav")) show(pic) blockingPlay(snd) pic = makePicture(getMediaPath("santa.jpg")) snd = makeSound(getMediaPath("bassoon-g4.wav")) show(pic) blockingPlay(snd) pic = makePicture(getMediaPath("jungle2.jpg")) snd = makeSound(getMediaPath("bassoon-c4.wav")) show(pic) blockingPlay(snd)

Page 8: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

What’s wrong with this? From Procedural Abstraction:

We have duplicated code. We should get rid of it.

From Object-Oriented Programming: We have an object: A slide.

Page 9: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

The Slide Object What does a slide know?

It has a picture. It has a sound

What can a slide do? Show itself.

Show its picture. (Blocking) Play its sound.

Page 10: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

We’ve been doing this already, of course. You’ve been using objects already,

everywhere. Pictures, sounds, samples, colors—these are

all objects. We’ve been doing aggregation.

We’ve worked with or talked about lists of pictures, sounds, pixels, and samples

The functions that we’ve been providing merely cover up the underlying objects.

Page 11: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Using picture as an object>>>

pic=makePicture(getMediaPath("barbara.jpg"))

>>> pic.show()

Page 12: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

What are Turtles?

Turtles are objects.

In JES, a turtle is represented as a small image of a turtle composed of a shell and a body.

Page 13: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

What are Worlds? Worlds are where

turtles are displayed.

Worlds have height and width similar to images.

One world can holdmultiple turtles.

A world = window where the turtle is

Page 14: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Making Turtles and Worlds

To make a turtle you must make a world first.

Make a world using the makeWorld(width, height) function

ex.world = makeWorld(200, 200)

Once you have a world use the makeTurtle(world) function

ex.turtle = makeTurtle(world)

Page 15: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Start with a world(s) makeWorld() will create a default sized window 640 x 480 pixels makeWorld(width, height) can be used to get the size window that you want give your window a name:

world = makeWorld(200, 300)ormoshPit = makeWorld(400, 400)

You can actually have more than one window being active!

footballField = makeWorld(360, 160)danceFloor = makeWorld(200, 200)quarterback = makeTurtle(footballField)fullback = makeTurtle(footballField)dancer1 = makeTurtle(danceFloor)dancer2 = makeTurtle(danceFloor)

Page 16: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

What Can You Do With Turtles? Once you have a turtle you can tell it to do things or change its

appearance by using methods. Methods require dot notation similar to modules. Here is an

example of using methods to turn and move a turtle.

Page 17: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Moving Turtles Methods for moving turtles:

forward() , moves a turtle forward by 100 pixels.

forward(x) , moves a turtle forward by x pixels

backward() , moves a turtle backward 100 pixels.

backward(x) , moves a turtle backward by x pixels.

moveTo(x, y) , moves a turtle to the x and y position in the world.

Page 18: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Turning Turtles Methods for turning/rotating

turtles:

turnRight() , rotates a turtle 90 degrees to the right.

turnLeft() , rotates a turtle 90 degrees to the left.

turn(d) , rotates a turtle by d number of degrees to the right. A negative number will rotate the turtle to the left.

turnToFace(turtle) , rotates a turtle face another turtle.

turnToFace(x,y) , rotates a turtle to an x and y position on the world.

Page 19: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Modifying Turtles Methods for changing the

appearance of turtles:

setColor(c) , changes a turtle's shell and body color to color c.

setShellColor(c) , changes a turtle's shell color to color c.

setBodyColor(c), changes a turtle's body color to color c.

setWidth(width), changes the width of a turtle.

setHeight(height), changes the height of a turtle.

Page 20: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Turtle Visibility

If you do not want to see a turtle anymore you can hide it.

Methods for changes the visibility settings of turtles:

hide(), stops displaying a turtle on the world. (Just makes it invisible – it is still there.)

show(), begins displaying a turtle on the world if that turtle is hidden.

Page 21: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

The Pen Turtles normally leave a trail

when they move. You can control aspects of turtle's trail. Methods to control a turtle's pen:

penUp() , tells a turtle to stop making a trail.

penDown() , tells a turtle to begin making a trail.

setPenColor(c) , changes a turtle's trail color to color c.

setPenWidth(w) , changes a turtle's trail width to w.

Page 22: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Dropping Images Turtles can drop images onto the

world. This can be useful if you your want your world to have a background image.

First you will have to make an image using the makePicture(file) function.ex. image = makePicture(“fluffy.jpg”)

Then use the method drop(image) to drop the image onto the world.

The top left corner of the image will be the location of the turtle.

Page 23: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Turtle Choreography

A simple way to create an animation using turtles is by using for loops and the time module.

First import the time module:ex.import time

Create a for loop that tells a turtle to repeat an action, and end the for loop with a time.sleep() function.

time.sleep(s) , creates a delay s seconds long before continuing.

Page 24: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

See list of commands

See commands from JES.

Click "JES Functions"->"Turtles"

We'll be using Object-oriented style when calling the turtle functions:

fred.forward(95)instead of forward(fred, 95)

Page 25: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Worlds know what turtles are in them

Each time we add a turtle, the world keeps track

We can ask a world to tells what turtles are in it: getTurtleList(world)

It returns an array (linear collection) of turtles objects!

This is similar to getPixels(pic), which returns a linear collection of pixel objects

Page 26: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Recall what we know about operating on lists

We can access an element in the list using [ ] We can iterate (loop) over a list of turtles just

like we loop over a list of numbers Recall how the for loop works!

Runs once for every element in the list First time around stores the first element in the

loop variable Second time around, stores the second element in

the loop variable This continues until we get to the end of the list

Page 27: Object-Oriented Programming. Object-oriented programming  First goal: Define and describe the objects of the world  Noun-oriented  Focus on the domain

Turtles know where they are in the world

Get a turtle’s position very easily… just ask it!

getXPos() getYPos()