Exercises


[Page 599 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 12.1

Explain the difference between the following pairs of terms:

  1. Iteration and recursion.

  2. Recursive method and recursive definition.

  3. Base case and recursive case.

  4. Head and tail.

  5. Tail and nontail recursive.

Exercise 12.2

Describe how the method call stack is used during a method call and return.

Exercise 12.3

Why is a recursive algorithm generally less efficient than an iterative algorithm?

Exercise 12.4

A tree, such as a maple tree or pine tree, has a recursive structure. Describe how a tree's structure displays self-similarity and divisibility.

Exercise 12.5

Write a recursive method to print each element of an array of double.

Exercise 12.6

Write a recursive method to print each element of an array of double from the last to the first element.

Exercise 12.7

Write a recursive method that will concatenate the elements of an array of String into a single String delimited by blanks.

Exercise 12.8

Write a recursive method that is passed a single int parameter, N 0, and prints all the odd numbers between 1 and

Exercise 12.9

Write a recursive method that takes a single int parameter N 0 and prints the sequence of even numbers from

Exercise 12.10

Write a recursive method that takes a single int parameter N 0 and prints the multiples of 10 between 0 and


[Page 600]
Exercise 12.11

Write a recursive method to print the following geometric pattern:

# # # # # # # # # # # # # # # 


Exercise 12.12

Write recursive methods to print each of the following patterns.

# # # # # # # #    # # # # # # # #   # # # # # # #    # # # # # # #     # # # # # #    # # # # # #       # # # # #    # # # # #         # # # #    # # # #           # # #    # # #             # #    # #               #    # 


Exercise 12.13

Write a recursive method to print all multiples of M up to M * N.

Exercise 12.14

Write a recursive method to compute the sum of the grades stored in an array.

Exercise 12.15

Write a recursive method to count the occurrences of a substring within a string.

Exercise 12.16

Write a recursive method to remove the HTML tags from a string.

Exercise 12.17

Implement a recursive version of the Caesar.decode() method from Chapter 8.

Exercise 12.18

The Fibonacci sequence (named after the Italian mathematician Leonardo of Pisa, ca. 1200) consists of the numbers 0,1,1,2,3,5,8,13,. . . in which each number (except for the first two) is the sum of the two preceding numbers. Write a recursive method fibonacci(N) that prints the first N Fibonacci numbers.

Exercise 12.19

Write a recursive method to rotate a String by N characters to the right. For example, rotateR("hello," 3) should return "llohe".

Exercise 12.20

Write a recursive method to rotate a String by N characters to the left. For example, rotateL("hello," 3) should return "lohel".

Exercise 12.21

Write a recursive method to convert a String representing a binary number to its decimal equivalent. For example, binTodecimal("101011") should return the int value 43.

Exercise 12.22

A palindrome is a string that is equal to its reverse"mom", "i", "radar", and "able was i ere i saw elba". Write a recursive boolean method that determines whether its String parameter is a palindrome.


[Page 601]
Exercise 12.23

Challenge: Incorporate a drawBinaryTree() method into the RecursivePatterns program. A level-one binary tree has two branches. At each subsequent level, two smaller branches are grown from the endpoints of every existing branch. The geometry is easier if you use 45-degree angles for the branches. Figure 12.36 shows a level-four binary tree drawn upside down.

Figure 12.36. A level-four binary tree pattern.


Exercise 12.24

Challenge: Towers of Hanoi. According to legend, some Buddhist monks were given the task of moving 64 golden disks from one diamond needle to another needle, using a third needle as a backup. To begin with, the disks were stacked one on top of the other from largest to smallest (Fig. 12.37). The rules were that only one disk could be moved at a time and that a larger disk could never go on top of a smaller one. The end of the world was supposed to occur when the monks finished the task!

Figure 12.37. The towers of Hanoi problem. Move all the disks from needle A to needle B. Only one disk can be moved at a time, and a larger disk can never go on top of a smaller one.


Write a recursive method, move(int N, char A, char B, char C), that will print out directions the monks can use to solve the towers of Hanoi problem. For example, here's what it should output for the three-disk case, move(3, "A", "B", "C"):

Move 1 disk from A to B. Move 1 disk from A to C. Move 1 disk from B to C. Move 1 disk from A to B. Move 1 disk from C to A. Move 1 disk from C to B. Move 1 disk from A to B. 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net