Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Objects in Action 123

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


For another example of OOP, consider the autodialerprogram that Matthew
Broderick’s character used in the movie WarGamesto 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 por-
trayed by Broderick) used his autodialer to look for a video game compa-
ny’s private computer system—he wanted to play the company’s new game
before it was released. Instead, Lightman found a secret government com-
puter 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:


. AModemobject, 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
. AMonitorobject, 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 Modemobject is that
it could be used in other programs that needmodem 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 Modemobject and you know it’s not dependent
on anything else, you can focus on making sure the Modemobject 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 program-
ming language can be advantageous because you’re not unlearning the
habits of other styles of programming.


NOTE
Anautodialeris 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.
Free download pdf