Interactive Lesson

Objects

Supplemental Material for Baldwin and Scragg, Algorithms and Data Structures: The Science of Computing; Charles River Media, 2004 (Now published by Cengage Learning)

Site Index


This lesson supplements the introduction to objects and messages in sections 2.1.1 and 2.1.2 of Algorithms and Data Structures: The Science of Computing. Read these sections while doing this exercise.

Objects

Think of an object as an agent that can help accomplish some task. Designing an object oriented algorithm then amounts to selecting a set of agents and coordinating their actions so that they solve the problem at hand.

Two things influence an object’s role in an algorithm: the object’s behavior and its state.

Behavior is simply what an object is able to do, the actions it can perform as a problem-solving agent. Every object has a fixed set of actions it can perform. Objects perform actions in response to messages, signals sent to the object that tell it to perform a specific action. Messages can be accompanied by parameters, pieces of data that the object will use in carrying out the action associated with the message.

State is a general notion of the object’s value — state can include things like where the object is, what it has done in the recent past, etc., as well as more common kinds of value such as a number stored within the object. Many of the actions an object performs change its state in some way. Similarly, the details of how the object carries out some of its actions may depend on its state.

Exercise

Four “woggles,” named “Allie,” “Bo,” “Callie,” and “Del,” appear below. A woggle is an object whose state consists of its size and its color. Woggles change their state in response to the following messages:

inflate
Makes a woggle bigger. But if a woggle gets too big, it becomes unhappy and refuses to grow any more.
deflate
Makes a woggle smaller. But if a woggle gets too small, it becomes unhappy and refuses to shrink any more.
colorize
Changes a woggle’s color. The new color (white, pink, yellow, or blue) is a parameter to this message.

Your job is to send sequences of messages to the woggles in order to solve certain problems. To send a message to a woggle, select the woggle and message (and parameter, if the message is “colorize”) from the pull-down menus below the woggles, then click the “Send” button. The woggles will show you the effects of your messages.

Problems, along with any hints or feedback you request, appear immediately above the “Next,” “Hint,” and “Show Me” buttons. Problems are generated randomly, and you can do as many or as few as you wish. At any time, you can…

Sorry, but the interactive lesson cannot be displayed.

Review

This exercise has showed you…


Copyright © 2004 Charles River Media. All rights reserved.

Revised Aug. 8, 2005 by Doug Baldwin

Site Index