Programming and Problem Solving with Java

(やまだぃちぅ) #1

13


CHAPTER


any method can call another method. A method can even call it-
self! When a method calls itself, it makes a recursive call. The word recur-
sivemeans “having the characteristic of coming up again, or repeating.”
In this case, a method call is repeated by the method itself. Recursion is
a powerful technique that can be used in place of iteration
(looping).
Recursive solutions are generally less efficient than it-
erative solutions to the same problem. However, some prob-
lems lend themselves to simple, elegant, recursive solutions
and are exceedingly cumbersome to solve iteratively. Some
programming languages, such as early versions of FORTRAN,
BASIC, and COBOL, do not support recursion. Other languages
are especially oriented to recursive algorithms—LISP, for example. Java
lets us take our choice: We can implement both iterative and recursive al-
gorithms.
Our examples are broken into two groups: problems that use only
simple variables and problems that use structured variables. If you are
studying recursion before reading Chapter 10 on structured data types,
then cover only the first set of examples and leave the rest until you have
completed Chapters 10 through 12.
Rather than examine one large Case Study at the end of the chapter,
we solve several small problems using recursion throughout the chapter.


In Java,

1992
The first 64-bit chip
is introduced by
DEC

1993
Apple Computer
announces the
Newton, a personal
digital assistant
(PDA) with
handwriting
recognition
capabilities

1993
Intel introduces the
Pentium Chip

1994
Jim Clark and Marc
Andreesen create
Netscape
Communications
(their first browser),
contributing to a
growing population
of web surfers

1995
Toy Storyis
produced from the
Pixar division of
Disney and is the
first full-length
computer-generated
feature film. It
receives rave
reviews

1995
Sun Microsystems
introduces the
object-oriented
programming
language Java™

Recursive call A method call
in which the method being
called is the same as the one
making the call
Free download pdf