Chapter 16 Study Guide

CSC 200 Study Guide – Fall 2010

Chapter 16 Object-Oriented Programming

Chapter Learning Objectives

·  To use object-oriented programming to make programs easier to develop in teams, more robust, and easier to debug.
·  To understand such features of object-oriented programs as polymorphism, encapsulation, inheritance and aggregation.
·  To be able to choose between different styles of programming for different purposes.

Study Smart, Not Hard

Learn the information as we go through the semester. Spend a hour or so a day a week before the exam to study. It is impossible to cram all this information into your brain if you wait until night before the exam. Read all the short programs in this chapter to make sure that you understand exactly what they do. You should understand them much easier now than you did earlier this semester.

16.1 History of Objects

See Chapter 15 Study Guide for more details on the history of programming languages.
Object-Oriented Programming Model (OOPs) is the newest language paradigm. This model views the world as a collection of interacting objects. Each object is defined by a class, and the actions are defined by methods. These languages allow the programmer to express algorithms using a hierarchy of objects.
Alan Kay invented the term object-oriented programming and created the first object-oriented language called SmallTalk. Alan earned the 2003 Turing Award for his pioneering world in OOPs. (Note: Book has wrong date) One of his mentors (during graduate school) was Ivan Sutherland, who had done pioneering graphics programs including Sketchpad. “Software is actually a simulation of world. By making software model the world, it becomes clearer how to make software.”
Adele Goldberg also worked on the SmallTalk project and was a co-author of the first book about SmallTalk.
Benefits of Object-Oriented Programming
1.  Manage complexity - each object is self-contained so it is much easier to debug errors and manage the project.
2.  Supports robustness - Robustness is the ability of a computer system to cope with errors during execution or the ability of an algorithm to continue to operate despite abnormalities in input or calculations.
3.  Supports reuse - This means you can reuse objects (and code) more easily than traditional programming methods. Inheritance in OOPs naturally creates reusability.

16.2 Working with Turtles

(Page 361) Dr. Seymour Papert, at MIT, created a system called Logo that programmed turtles on a screen to help young children think and solve problems in the late 1960s.
(Not in book) Karel the Robot was another popular Logo type language created and used at Stanford University by Richard E. Pattis in 1981. This language used a robot called Karel instead of turtles. This program was taught in Computer Science classes at NRCC and RU in the 1990s. See the Karel the Robot link for example code. It looks very similar to JES.
In this section, you learn how to use the Turtle objects available in JES.
1.  To start a turtle program in JES, you must create the world, and create an instance of the world.
2.  Next you create and name an instance of a turtle.
3.  Now you can send messages to the turtle and make it move on the screen and draw.
See the Built-in JES for Turtle Objects (later in this study guide) for details on creating worlds, turtle and methods.

16.3 Teaching Turtles New Tricks

In this section, you learn how to create a subclass of a turtle and teach it new tricks.
See Programs 146 and 147 on pages 369 – 370 for details on creating a subclass called smartTurtle from the built-in turtle class/object.

16.4 An Object-Oriented Slide Show

·  Skip this entire section (pages 371 – 380) except for the definition of encapsulation (on page 373) and polymorphism (on page 376).

16.4.3 Why Objects?

In this section, more benefits of OOPs are described. Object-oriented programming is great for GUI applications, interactive games and interactive applications. Some benefits are listed in Section 16.1 (earlier in this study guide). Other benefits are PIE, polymorphism, inheritance, and encapsulation.

Built-in JES for Turtle Objects

·  makeWorld()
(page 362) creates a new world instance for turtles to explore.
·  makeTurtle(worldName)
(page 363) creates a new turtle instance that appears in the center of the world
·  .forward() or .forward(pixels)
(page 363) method that moves the turtle forward 100 pixels (default) or the number of pixels specified
·  .turnLeft() or turnRight()
(page 364) method that turns the turtle left or right
·  .turn(degrees)
(page 364) method that turns the turtle the specified number of degrees. Remember 360 degrees is a full spin.
·  .penUp() or .penDown()
(page 366) method that for the turtle to pick up or put down a drawing pen
·  .moveTo(x,y)
(page 367) method that moves a turtle to the specified coordinate position
·  .setPenWidth(width)
(page 367) method that sets the turtle pen to the specified width
·  .setColor(color)
(page 367) method that changes the turtle pen to the specified color

Key Terms

Make sure that you understand the definition of the following terms. The page is given from the textbook. Each term is either bold or italic in the textbook. Use your textbook and other sources such as webopedia.com , Wikipedia.org or your favorite search engine to add the definitions to the list.
Remember OOP is easy as PIE. A language must suuport Polymorphism, Inheritance and Encapsulation to be classified as a OOP language.
Page / Term / Definition
360 / Object-oriented programming / See previous page of this study guide.
361 / object / The primary items in an object oriented program. These are the nouns in the system you are modeling.
class / The formal definition of an object. Similar to the function definitions we have been using all semester.
361 / Instance / An instance is an occurrence or an object in a program. Analogy: The class is the recipe for an object such as a cake and the baked cake would be the instance.
361 / Method / Methods are the verbs in an OOP project. The methods are associated with a particular object. For example if the object was rabbit, then methods could be hop, dig(hole), eat(carrots), wiggle(nose).
376 / Polymorphism / Polymorphism means that you can have methods with the exact same name that will do the correct process for different objects or even in the same object. See Program 147 smartTurtle (page 147) for an example. We have 2 methods named drawSquare but it executes the correct method based on the number of inputs.
381 / Inheritance / Inheritance is a powerful feature where an object can inherit all the methods and properties of a parent object. See Program 146 smartTurtle (page 369) for an example. All the methods that a turtle object can do such as moveTo, forward, turnRight etc. are available to the smartTurtle because it is a subclass(child) of the turtle object built into JES.
373 / Encapsulation / Encapsulation means that the object properties and methods are all defined in a single class (section of code). This makes it much easier to debug and program. See Program 147 smartTurtle (page 147) for an example. All the methods for the smartTurtle are contained within the class code.

Jython / JES Programming Summary

·  Ignore the Programming Summary in the book as these are the graphic commands from the slide show section that we skipped.
Last Update: December 3, 2010
Study Guide created by Carlotta Eaton
For Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition


CSC 200 Page 5