Java Reference
In-Depth Information
9.4
( Occurrences of a specified character ) Write a method that finds the number of
occurrences of a specified character in a string using the following 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 followed by a character and displays the number
of occurrences of the character in the string.
**9.5
( Occurrences of each digit in a string ) Write a method that counts the occurrences
of each digit in a string using the following header:
public static int [] count(String s)
The method counts how many times a digit appears in the string. The return value
is an array of ten elements, each of which holds the count for a digit. For example,
after executing int[] counts = count("12203AB3") , counts[0] is 1 ,
counts[1] is 1 , counts[2] is 2 , and counts[3] is 2 .
Write a test program that prompts the user to enter a string and displays the num-
ber of occurrences of each digit in the string.
*9.6
( Count the letters in a string ) Write a method that counts the number of letters in a
string using the following header:
public static int countLetters(String s)
Write a test program that prompts the user to enter a string and displays the num-
ber of letters in the string.
*9.7
( Phone keypads ) The international standard letter/number mapping found on the
telephone is:
1
2
3
ABC
DEF
4
5
6
GHI
JKL
MNO
7
8
9
PQRS
TUV
WXYZ
0
Write a method that returns a number, given an uppercase letter, as follows:
public static int getNumber( char uppercaseLetter)
Write a test program that prompts the user to enter a phone number as a string. The
input number may contain letters. The program translates a letter (upper- or lower-
case) to a digit and leaves all other characters intact. Here is a sample run of the
program:
Enter a string:
1-800-3569377
1-800-Flowers
 
Search WWH ::




Custom Search