19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

762 Chapter 20 Recursion<br />

*20.16 (Find the number of uppercase letters in an array) Write a recursive method <strong>to</strong><br />

return the number of uppercase letters in an array of characters. You need <strong>to</strong><br />

define the following two methods. The second one is a recursive helper method.<br />

public static int count(char[] chars)<br />

public static int count(char[] chars, int high)<br />

Write a test program that prompts the user <strong>to</strong> enter a list of characters in one<br />

line and displays the number of uppercase letters in the list.<br />

*20.17 (Occurrences of a specified character in an array) Write a recursive method that<br />

finds the number of occurrences of a specified character in an array. You need <strong>to</strong><br />

define the following two methods. The second one is a recursive helper method.<br />

public static int count(char[] chars, char ch)<br />

public static int count(char[] chars, char ch, int high)<br />

Write a test program that prompts the user <strong>to</strong> enter a list of characters in one line,<br />

and a character, and displays the number of occurrences of the character in the list.<br />

Sections 20.6–20.10<br />

*20.18 (Towers of Hanoi) Modify Listing 20.8, TowersOfHanoi.java, so that the program<br />

finds the number of moves needed <strong>to</strong> move n disks from <strong>to</strong>wer A <strong>to</strong> <strong>to</strong>wer<br />

B. (Hint: Use a static variable and increment it every time the method is called.)<br />

*20.19 (Sierpinski triangle) Revise Listing 20.9 <strong>to</strong> develop an applet that lets the user<br />

use the + and - but<strong>to</strong>ns <strong>to</strong> increase or decrease the current order by 1, as<br />

shown in Figure 20.12a. The initial order is 0. If the current order is 0, the<br />

Decrease but<strong>to</strong>n is ignored.<br />

(a)<br />

(b)<br />

FIGURE 20.12 (a) Exercise 20.19 uses the + and - but<strong>to</strong>ns <strong>to</strong> increase or decrease the<br />

current order by 1. (b) Exercise 20.20 draws ovals using a recursive method.<br />

*20.20 (Display circles) Write a <strong>Java</strong> applet that displays ovals, as shown in Figure<br />

20.12b. The ovals are centered in the panel. The gap between two adjacent<br />

ovals is 10 pixels, and the gap between the border of the panel and the largest<br />

oval is also 10.<br />

*20.21 (Decimal <strong>to</strong> binary) Write a recursive method that converts a decimal number<br />

in<strong>to</strong> a binary number as a string. The method header is:<br />

public static String decimalToBinary(int value)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!