Web hosting unlimited bandwidth - Chapter 6 Methods 281 factorial method produces large
Chapter 6 Methods 281 factorial method produces large values so quickly that even long does not help us print many factorial values before the values exceed the size that can be stored in a long variable. We explore in the exercises the fact that float and double may ultimately be needed by users desiring to calculate factorials of larger numbers. This situation points to a weakness in most programming languages, namely, that the languages are not easily extended to handle the unique requirements of various applications. As we will see in Chapter 9, Object-Oriented Programming, Java is an extensible language that allows us to create arbitrarily large integers if we wish. In fact, package java.math provides two classes BigInteger and BigDecimal explicitly for mathematical calculations of arbitrary precision that cannot be represented with Java s primitive data types. Common Programming Error 6.15 Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution. 6.13 Example Using Recursion: The Fibonacci Series The Fibonacci series, 0,1, 1, 2, 3, 5, 8, 13,21, begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers. The series occurs in nature and, in particular, describes a form of spiral. The ratio of successive Fibonacci numbers converges on a constant value of 1.618 . This number, too, repeatedly occurs in nature and has been called the golden ratio, or the golden mean. Humans tend to find the golden mean aesthetically pleasing. Architects often design windows, rooms and buildings whose length and width are in the ratio of the golden mean. Postcards are often designed with a golden-mean length/width ratio. The Fibonacci series may be defined recursively as follows: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n 1) + fibonacci(n 2) Note that there are two base cases for the Fibonacci calculation: fibonacci(0) is defined to be 0, and fibonacci(1) is defined to be 1. The applet of Fig. 6.13 calculates the ith Fibonacci number recursively, using method fibonacci (lines 68 78). The applet enables the user to input an integer in a JTextField. The value input indicates the ith Fibonacci number to calculate. When the user presses the Enter key, method actionPerformed executes in response to the user interface event and calls method fibonacci to calculate the specified Fibonacci number. Fibonacci numbers tend to become large quickly. Therefore, we use data type longas the parameter type and the return type of fibonacci. In Fig. 6.13, the screen captures show the results of calculating several Fibonacci numbers. Once again, method init of this applet creates the GUI components and attaches them to the applet s content pane. The layout manager for the content pane is set to Flow- Layout at line 22. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01