Java Reference
In-Depth Information
E XERCISES
1.
How does a dictionary differ from a sorted list?
2.
Implement a method for the class TelephoneDirectory —described in Segment 19.9—that adds an entry to
the directory, given the person's name and telephone number. The method should return true if the entry was
added. If the person is already in the directory, the method should replace the person's telephone number and
return false.
3.
Implement a method for the telephone directory problem of Segment 19.9 to display everyone's name and
telephone number.
4.
In the telephone directory problem of Segment 19.9, the case of the letters in a name affects the name's order in
the dictionary. What steps can you take so that case variations in the input file do not affect this order?
5.
In the telephone directory problem of Segment 19.9, suppose that the text file of names and telephone numbers is
sorted by name.
a. What impact would this aspect of the file have on the efficiency of the method readFile for various
implementations of the dictionary?
b. Would it matter whether the file was in reverse alphabetical order?
6.
A reverse directory allows one to search for the name corresponding to a given telephone number. Modify the
class TelephoneDirectory of Segment 19.9 to give it this capability. Use a second dictionary as the reverse
directory. Add a query method and modify the method readFile accordingly.
7.
Draw a class diagram for the class FrequencyCounter , as outlined in Segment 19.13, that is analogous to the
diagram in Figure 19-4 of Segment 19.8.
8.
The word-frequency problem of Segment 19.12 finds the frequency with which each distinct word occurs within
some given text. Describe the changes that you could make to the class FrequencyCounter if you wanted to list
the words that occur for each frequency.
9.
Repeat Exercise 7 for the class Concordance , as outlined in Segment 19.19.
10.
In the concordance problem of Segment 19.18, if a word occurs more than once in a single line, the number of that
line appears more than once in the concordance. Revise the Java code given in Segment 19.20 so that the line
numbers associated with a given word are distinct.
11.
Design an ADT that stores the side effects of various drugs. Each drug should have a list of associated side effects.
Provide a method that returns the side effects of a given drug. Then use a dictionary to implement the class
DrugSideEffects .
12.
Consider a look-up service for the television shows on a given date. A file contains information about these
shows. Each show's data appears on two lines. The first line gives the name of the station, the channel, the start
time, the stop time, the name of the show, and a rating. These entries are separated by tildes (~), and the times are
in 24-hour notation (for example, 1 p. m. is 13:00). The second line briefly describes the show.
Implement a method with the header
public void readFile(Scanner data)
to read the file into a dictionary that will be searched. Decide what data should be the search key and what should
be the associated value. Design any classes needed for the key and the value.
Search WWH ::




Custom Search