19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.4 (Occurrences of a specified character) Write a method that finds the number of<br />

occurrences of a specified character in a string using the following header:<br />

public static int count(String str, char a)<br />

For example, count("Wel<strong>com</strong>e", 'e') returns 2. Write a test program that<br />

prompts the user <strong>to</strong> enter a string followed by a character and displays the number<br />

of occurrences of the character in the string.<br />

**9.5 (Occurrences of each digit in a string) Write a method that counts the occurrences<br />

of each digit in a string using the following header:<br />

public static int[] count(String s)<br />

The method counts how many times a digit appears in the string. The return value<br />

is an array of ten elements, each of which holds the count for a digit. For example,<br />

after executing int[] counts = count("12203AB3"), counts[0] is 1,<br />

counts[1] is 1, counts[2] is 2, and counts[3] is 2.<br />

Write a test program that prompts the user <strong>to</strong> enter a string and displays the number<br />

of occurrences of each digit in the string.<br />

*9.6 (Count the letters in a string) Write a method that counts the number of letters in a<br />

string using the following header:<br />

public static int countLetters(String s)<br />

Write a test program that prompts the user <strong>to</strong> enter a string and displays the number<br />

of letters in the string.<br />

*9.7 (Phone keypads) The international standard letter/number mapping found on the<br />

telephone is:<br />

<strong>Programming</strong> Exercises 363<br />

1 2 3<br />

ABC<br />

DEF<br />

4 5 6<br />

GHI JKL MNO<br />

7 8 9<br />

PQRS TUV WXYZ<br />

0<br />

Write a method that returns a number, given an uppercase letter, as follows:<br />

public static int getNumber(char uppercaseLetter)<br />

Write a test program that prompts the user <strong>to</strong> enter a phone number as a string. The<br />

input number may contain letters. The program translates a letter (upper- or lowercase)<br />

<strong>to</strong> a digit and leaves all other characters intact. Here is a sample run of the<br />

program:<br />

Enter a string:<br />

1-800-3569377<br />

1-800-Flowers

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

Saved successfully!

Ooh no, something went wrong!