Java Reference
In-Depth Information
*20.16
( Find 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
define 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)
Write a test program that prompts the user to enter a list of characters in one
line and displays the number of uppercase letters in the list.
*20.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
define 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)
Write a test program that prompts the user to enter a list of characters in one line,
and a character, and displays the number of occurrences of the character in the list.
Sections 20.6-20.10
*20.18
( Towers of Hanoi ) Modify Listing 20.8, TowersOfHanoi.java, so that the pro-
gram 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.)
*20.19
( Sierpinski triangle ) Revise Listing 20.9 to develop an applet that lets the user
use the and buttons to increase or decrease the current order by 1 , as
shown in Figure 20.12a. The initial order is 0 . If the current order is 0 , the
Decrease button is ignored.
+
-
(a)
(b)
F IGURE 20.12 (a) Exercise 20.19 uses the and buttons to increase or decrease the
current order by 1 . (b) Exercise 20.20 draws ovals using a recursive method.
+
-
*20.20
( Display circles ) Write a Java applet that displays ovals, as shown in Figure
20.12b. The ovals are centered in the panel. The gap between two adjacent
ovals is 10 pixels, and the gap between the border of the panel and the largest
oval is also 10 .
*20.21
( Decimal to binary ) Write a recursive method that converts a decimal number
into a binary number as a string. The method header is:
public static String decimalToBinary( int value)
 
 
Search WWH ::




Custom Search