19.1 | ( Computing factorials ) Rewrite the factorial method in Listing 19.1 using iterations. | ||
19.2* | ( Fibonacci numbers ) Rewrite the fib method in Listing 19.2 using iterations. Hint
| ||
19.3* | ( Computing greatest common divisor using recursion ) The gcd(m, n) can also be defined recursively as follows:
Write a recursive method to find the GCD. Write a test program that computes gcd(24, 16) and gcd(255, 25) . | ||
19.4 | ( Summing series ) Write a recursive method to compute the following series:
| ||
19.5 | ( Summing series ) Write a recursive method to compute the following series:
| ||
19.6** | ( Summing the series ) Write a recursive method to compute the following series:
| ||
19.7* | ( Fibonacci series ) Modify Listing 19.2, ComputeFibonacci.java, so that the program finds the number of times the fib method is called. Hint
|
19.8** | ( Printing the digits in an integer reversely ) Write a recursive method that displays an int value reversely on the console using the following header: public static void reverseDisplay( int value) For example, reverseDisplay(12345) displays 54321 . |
| |
19.9** | ( Printing the characters in a string reversely ) Write a recursive method that displays a string reversely on the console using the following header: public static void reverseDisplay(String value) For example, reverseDisplay("abcd") displays dcba . |
19.10* | ( Occurrences of a specified character in a string ) Write a recursive method that finds the number of occurrences of a specified letter in a string using the following method header: public static int count(String str, char a) For example, count("Welcome", 'e') returns 2. |
19.11** | ( Summing the digits in an integer using recursion ) Write a recursive method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits( long n) For example, sumDigits(234) returns |
19.12** | ( Printing the characters in a string reversely ) Rewrite Exercise 19.9 using a helper method to pass the substring high index to the method. The helper method header is: public static void reverseDisplay(String value, int high) |
19.13** | ( Finding the largest number in an array ) Write a recursive method that returns the largest integer in an array. |
19.14* | ( Finding the number of uppercase letters in a string ) Write a recursive method to return the number of uppercase letters in a string. |
19.15* | ( Occurrences of a specified character in a string ) Rewrite Exercise 19.10 using a helper method to pass the substring high index to the method. The helper method header is: public static int count(String str, char a, int high) |
19.16* | ( Finding the number of uppercase letters in an array ) Write a recursive method to return the number of uppercase letters in an array of characters. You need to declare the following two methods. The second one is a recursive helper method. public static int count( char [] chars) public static int count( char [] chars, int high) |
19.17* | ( Occurrences of a specified character in an array ) Write a recursive method that finds the number of occurrences of a specified character in an array. You need to declare the following two methods. The second one is a recursive helper method. public static int count( char [] chars, char ch) public static int count( char [] chars, char ch, int high) |
19.18* | ( Towers of Hanoi ) Modify Listing 19.7, TowersOfHanoi.java, so that the program finds the number of moves needed to move n disks from tower A to tower B. (Hint: Use a static variable and increment it every time the method is called.) |
19.19* | ( Sierpinski triangle ) Revise Listing 19.8 to develop an applet that lets the user use the Increase and Decrease buttons to increase or decrease the current order by 1, as shown in Figure 19.8(a). The initial order is 0. If the current order is 0, the Decrease button is ignored. Figure 19.8. (a) Exercise 19.19 uses the Increase and Decrease buttons to increase or decrease the current order by 1. (b) Exercise 19.10 draws ovals using a recursive method.
|
19.20* | ( Displaying circles ) Write a Java applet that displays ovals, as shown in Figure 19.8(b). The ovals are centered in the panel. The gap between two adjacent ovals is 10 pixels, and the gap between the panel and the largest oval is also 10. |
19.21*** | ( String permutation ) Write a recursive method to print all the permutations of a string. For example, for a string abc , the printout is
Hint
| ||
19.22*** | ( Creating a maze ) Write an applet that will find a path in a maze, as shown in Figure 19.9(a). The applet should also run as an application. The maze is represented by an 8 x 8 board. The path must meet the following conditions:
Figure 19.9. The program finds a path from the upper-left corner to the bottom-right corner.
| ||
19.23*** | ( Koch snowflake fractal ) The text presented the Sierpinski triangle fractal. In this exercise, you will write an applet to display another fractal, called the Koch snowflake , named after a famous Swedish mathematician . A Koch snowflake is created as follows:
|