15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Organizing a Program with Object-Oriented Programming 45<br />

this task involves flying a rocket to the moon, driving a car down a road, or<br />

controlling a walking robot to an electric outlet to recharge its batteries.<br />

Book I<br />

Chapter 2<br />

The more you divide a larger task into smaller tasks, the harder it can be to<br />

understand what problem you’re even trying to solve. This gets even worse<br />

when you start writing actual program commands.<br />

The two parts of most programs are the commands that tell the computer<br />

what to do and the data that the program manipulates. So if you wrote a program<br />

to identify the current speed and direction of a rocket, the commands<br />

would tell the computer how to retrieve this in<strong>for</strong>mation and the speed and<br />

direction would be the actual data the program uses.<br />

Different Methods<br />

<strong>for</strong> Writing<br />

Programs<br />

Essentially, program commands are separate from the data that they manipulate.<br />

If one part of a program manipulates data incorrectly, the rest of the<br />

program winds up using that contaminated data and you, as the programmer,<br />

won’t know which part of the program screwed up the data. This is like<br />

sitting in a baseball game, ordering a hot dog from a vendor, and having six<br />

people pass your hot dog down to you. When you see fingerprints all over<br />

your hot dog, can you tell which person touched your food?<br />

Objects isolate data<br />

Object-oriented <strong>programming</strong> avoids this problem by combining data and the<br />

commands that manipulate them into a single entity called (surprise!) an object.<br />

With object-oriented <strong>programming</strong> in the hot dog vendor analogy, instead of<br />

passing your hot dog to half a dozen other people, the hot dog vendor comes<br />

directly to your seat and hands you your hot dog. Now if you saw fingerprints<br />

all over your hot dog, you’d know that the fingerprints could only have come<br />

from the hot dog vendor.<br />

Besides keeping data isolated from other parts of your program, object-oriented<br />

<strong>programming</strong> also helps you divide a large program into smaller ones.<br />

Although structured <strong>programming</strong> divides a large program into the tasks<br />

that need to be per<strong>for</strong>med, object-oriented <strong>programming</strong> divides a large program<br />

into real-life objects.<br />

So if you were designing a program to launch a rocket to the moon, objectoriented<br />

<strong>programming</strong> would let you divide the program into objects. One<br />

object might be the rocket, a second object might be the moon, and a<br />

third object might be the Earth.<br />

You can also divide a large object into smaller ones. So the rocket object<br />

might be divided into an engine object and a guidance object. The engine<br />

object could be further divided into a fuel pump object, a nozzle object,<br />

and a fuel tank object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!