Java Reference
In-Depth Information
(a)
(b)
F IGURE 21.8
The Java code in plain text in (a) is displayed in HTML with syntax highlighted in (b).
Sections 21.5-21.7
*21.6
( Count the occurrences of numbers entered ) Write a program that reads an
unspecified number of integers and finds the one that has the most occurrences.
The input ends when the input is 0 . For example, if you entered 2340354-3
3320 , the number 3 occurred most often. If not one but several numbers have
the most occurrences, all of them should be reported. For example, since 9 and 3
appear twice in the list 93039324 , both occurrences should be reported.
**21.7
( Revise Listing 21.9, CountOccurrenceOfWords.java ) Rewrite Listing 21.9 to
display the words in ascending order of occurrence counts.
( Hint : Create a class named WordOccurrence that implements the Comparable
interface. The class contains two fields, word and count . The compareTo
method compares the counts. For each pair in the hash set in Listing 21.9, create
an instance of WordOccurrence and store it in an array list. Sort the array list
using the Collections.sort method. What would be wrong if you stored the
instances of WordOccurrence in a tree set?)
**21.8
( Count the occurrences of words in a text file ) Rewrite Listing 21.9 to read the
text from a text file. The text file is passed as a command-line argument. Words
are delimited by whitespace characters, punctuation marks ( ,;.:? ), quotation
marks ( '" ), and parentheses. Count words in case-insensitive fashion (e.g., con-
sider Good and good to be the same word). The words must start with a letter.
Display the output in alphabetical order of words, with each word preceded by
its occurrence count.
**21.9
( Guess the capitals using maps ) Rewrite Programming Exercise 8.37 to store
pairs of each state and its capital in a map. Your program should prompt the user
to enter a state and should display the capital for the state.
*21.10
( Count the occurrences of each keyword ) Rewrite Listing 21.7 CountKeywords.
java to read in a Java source code file and count the occurrence of each keyword
in the file, but don't count the keyword if it is in a comment or in a string literal.
**21.11
( Baby name popularity ranking ) Use the data files from Programming
Exercise 12.31 to write a program that enables the user to select a year, gender,
and enter a name to display the ranking of the name for the selected year and
gender, as shown in Figure 21.9. To achieve the best efficiency, create two arrays
for boy's names and girl's names, respectively. Each array has 10 elements for
10 years. Each element is a map that stores a name and its ranking in a pair
with the name as the key. Assume the data files are stored at www.cs.armstrong
.edu/liang/data/babynamesranking2001.txt , .  . . , and www.cs.armstrong.edu/liang/data/
babynamesranking2010.txt .
 
Search WWH ::




Custom Search