Objects in Action

Consider the case of the program that displays a pie chart. A PieChart object could consist of the following:

• Behavior to calculate the size of each pie slice

• Behavior to draw the chart

• An attribute to store the title of the chart

It might seem odd to ask the PieChart object to draw itself because graphs don’t draw themselves in the real world. Objects in OOP work for themselves whenever possible. This quality makes it easier to incorporate them in other programs. If a PieChart object did not know how to draw itself, for instance, every time you used that PieChart object in another program, you would have to create behavior to draw it.


Note

An autodialer is software that uses a modem to dial a series of phone numbers in sequence. The purpose of such a program is to find other computers that answer the phone, so you can call them later to see what they are.


For another example of OOP, consider the autodialer program that Matthew Broderick’s character used in the movie WarGames to find computers he could break into.

Using an autodialer today would attract the attention of your local phone company or law enforcement. Back in the ‘80s, it was a good way to be rebellious without leaving the house. David Lightman (the character portrayed by Broderick) used his autodialer to look for a video game company’s private computer system—he wanted to play the company’s new game before it was released. Instead, Lightman found a secret government computer that could play everything from chess to Global Thermonuclear War.

An autodialer, like any computer program, can be thought of as a group of objects that work together. It could be broken down into the following:

• A Modem object, which knows its attributes such as speed, and has behavior—for example, it can make the modem dial a number and detect that another computer system has answered a call

• A Monitor object, which keeps track of what numbers are called and which ones are successful

Each object exists independently of the other. 

One advantage of designing a completely independent Modem object is that it could be used in other programs that need modem functionality.

Another reason to use self-contained objects is that they are easier to debug. Computer programs quickly become unwieldy in size. If you’re debugging something like a Modem object and you know it’s not dependent on anything else, you can focus on making sure the Modem object does the job it’s supposed to do and holds the information that it needs to do its job.

Learning an object-oriented language such as Java as your first programming language can be advantageous because you’re not unlearning the habits of other styles of programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.71.115