Download presentation
Presentation is loading. Please wait.
Published byClyde Ball Modified over 9 years ago
1
Object-Oriented Programming
2
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…
3
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
4
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()
5
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.
6
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.
7
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)
8
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.
9
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.
10
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.
11
Using picture as an object >>> pic=makePicture(getMediaPath("barbara.jpg")) >>> pic.show()
12
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.
13
What are Worlds? Worlds are where turtles are displayed. Worlds have height and width similar to images. One world can hold multiple turtles. A world = window where the turtle is
14
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)
15
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) or moshPit = 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)
16
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.
17
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.
18
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.
19
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.
20
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.
21
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.
22
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.
23
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.
24
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)
25
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
26
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
27
Turtles know where they are in the world Get a turtle’s position very easily… just ask it! getXPos() getYPos()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.