Exercise Solutions, Ch. 12
Chapter 12 Exercise Solutions
EX 12.1. Write a recursive definition of a valid Java identifier (see Chapter 1).
A Java–Identifier is a: Letter
or a: Letter followed by a Java–Identifier–Substring
EX 12.2. Write a recursive definition of xy (x raised to the power y), where x and y are
integers and y > 0.
x1 = x
EX 12.3. Write a recursive definition of i * j (integer multiplication), where i > 0.
Define the multiplication process in terms of integer addition. For example, 4 * 7 is
equal to 7 added to itself 4 times.
1 * j = j
EX 12.4. Write a recursive definition of the Fibonacci numbers. The Fibonacci
numbers are a sequence of integers, each of which is the sum of the previous two
numbers. The first two numbers in the sequence are 0 and 1. Explain why you
would not normally use recursion to solve this problem.
Fib(0) = 0
EX 12.5. Modify the method that calculates the sum of the integers between 1 and N
shown in this chapter. Have the new version match the following recursive
definition: The sum of 1 to N is the sum of 1 to (N/2) plus the sum of (N/2 + 1) to N.
Trace your solution using an N of 7.
// Computes the sum of the numbers between n1 and n2 (inc)