Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^8) | Introduction to Object-Oriented Programming
Although a programming language is simple in form, it is not always easy to use. Try giving
someone directions to the nearest airport using a vocabulary limited to no more than 25
words, and you begin to see the problem. Programming forces you to write very simple, ex-
act instructions.
Translating an algorithm into a programming language is called codingthe
algorithm.Codeis the product of translating an algorithm into a programming
language. The term codecan refer to a complete program or to any portion of a pro-
gram. A program is tested by running (executing) it on the computer. If the program
fails to produce the desired results, the programmer must debugit—that is,
determine what is wrong and then modify the program, or even revise the algo-
rithm, to fix it. The process of coding and testing the algorithm takes place during
the implementation phase.
There is no single way to implement an algorithm. For example, an algorithm can be
translated into more than one programming language. Each translation produces a differ-
ent implementation (see Figure 1.2a). Even when two people translate an algorithm into the
same programming language, they often come up with different
implementations (see Figure 1.2b). Why? Because every
programming language allows the programmer some flexibility in
terms of how an algorithm is translated. Given this flexibility,
people adopt their own stylesin writing programs, just as they do
in writing short stories or essays. Once you have some program-
ming experience, you will develop a style of your own. Throughout
this book, we offer tips on good programming style.
Some people try to speed up the programming process by
going directly from the problem definition to coding (see Figure
1.3). Taking such a shortcut is very tempting and, at first glance,
seems to save a lot of time. However, for many reasons that will
become obvious as you read this book, this kind of shortcut actu-
ally consumes moretime and requires more effort. Developing a
general solution before you write Java code will help you manage
the problem, keep your thoughts straight, and avoid mistakes. If
you don’t take the time at the beginning to think out and polish
your algorithm, you’ll spend a lot of extra time debugging and
revising your code. So think first and code later! The sooner you
start coding, the longer it takes to write an application that works.
Once a Java application has been put into use, it often becomes
necessary to modify it later. Modification may involve fixing an
error that is discovered during use of the application or chang-
ing the code in response to changes in the user’s requirements.
Each time the code is modified, the programmer should repeat
the problem-solving and implementation phases for those aspects of the application that
change.
This phase of the programming process, known as maintenance, actually accounts for
Code Instructions for a com-
puter that are written in a pro-
gramming language
Algorithm
a. Algorithm translated into different languages
b. Algorithm translated by different people
Nell's Java Code
Nell's C++ Code
Nell's Ada Code
Algorithm
Nell's Java Code
Chip's Java Code
Mark's Java Code
Figure 1.2 Differences in Implementation

Free download pdf