Java Reference
In-Depth Information
*20.7
( Fibonacci series ) Modify Listing 20.2, ComputeFibonacci.java, so that the
program finds the number of times the fib method is called. ( Hint : Use a static
variable and increment it every time the method is called.)
Section 20.4
*20.8
( Print 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 . Write a test pro-
gram that prompts the user to enter an integer and displays its reversal.
*20.9
( Print the characters in a string reversely ) Write a recursive method that dis-
plays a string reversely on the console using the following header:
public static void reverseDisplay(String value)
For example, reverseDisplay("abcd") displays dcba . Write a test pro-
gram that prompts the user to enter a string and displays its reversal.
*20.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 fol-
lowing method header:
public static int count(String str, char a)
For example, count("Welcome", 'e') returns 2 . Write a test program that
prompts the user to enter a string and a character, and displays the number of
occurrences for the character in the string.
*20.11
( Sum 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 Write a test program
that prompts the user to enter an integer and displays its sum.
2
+
3
+
4
=
9.
Section 20.5
**20.12
( Print the characters in a string reversely ) Rewrite Exercise 20.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)
*20.13
( Find the largest number in an array ) Write a recursive method that returns the
largest integer in an array. Write a test program that prompts the user to enter a
list of eight integers and displays the largest element.
*20.14
( Find the number of uppercase letters in a string ) Write a recursive method to
return the number of uppercase letters in a string. Write a test program that prompts
the user to enter a string and displays the number of uppercase letters in the string.
*20.15
( Occurrences of a specified character in a string ) Rewrite Exercise 20.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)
 
Search WWH ::




Custom Search