about the file it represents (if any). 548
Exercise 20.10: Write a program that uses a StreamTokenizer object to break an input file into words and
counts the number of times each word occurs in the file, printing the result. Use a HashMap to keep track of
the words and counts. 548
Exercise 20.11: Using FilenameFilter or FileFilter, write a program that takes a directory and a
suffix as parameters and prints all files it can find that have that suffix. 549
Exercise 21.1: Write a program that opens a file and reads its lines one at a time, storing each line in a List
sorted by String.compareTo. The line-reading class you created for Exercise 20.4 should prove helpful.
584
Exercise 21.2: Rewrite the DataHandler class on page 457 to use a WeakHashMap to store the returned
data instead of a single WeakReference. 593
Exercise 21.3: A WeakHashMap has weak keys and strong values. A WeakValueMap would have strong
keys and weak values. Design a WeakValueMap. Be cautioned that this is not as simple as it might seem, in
fact it is extremely complicated and requires a number of design choices to be made. For example, should
iteration of values be allowed to yield null after hasNext has returned true, or should iteration keep the
values alive while they are being iterated? Hint: Don't try to extend AbstractMap, delegate to a HashMap
instead. 593
Exercise 21.4: Write a version of ShortStrings that implements ListIterator to filter a
ListIterator object. Should your class extend ShortStrings? 611
Exercise 21.5: Implement a more efficient ListIterator for ArrayBunchList. Be careful of the
specific contracts of ListIterator methods, such as set not being valid until either next or previous
is invoked. 616
Exercise 21.6: Rewrite the example program Concat on page 528 so that it uses an implementation of
Enumeration that has only one FileInputStream object open at a time. 617
Exercise 21.7: Implement a stack using ArrayList. Should your stack class be a subclass of ArrayList
or use an ArrayList internally, providing different stack-specific methods? 619
Exercise 22.1: Write a method that takes an array of floating-point values and a number indicating how many
columns to use, and prints the array contents. Try to ensure that the entries in each column line up neatly.
Assume a line is 80 characters wide. 632
Exercise 22.2: The WhichChars class has a problem marking characters near the top of the Unicode range
because the high character values will leave many unused bits in the lower ranges. Use a HashSet to solve
this problem by storing Character objects for each character seen. 635
Exercise 22.3: Now use a HashMap to store a BitSet object for each different top byte (high 8 bits)
encountered in the input string, with each BitSet storing the low bytes that have been seen with the
particular high byte. 635
Exercise 22.4: Provide an implementation of the Attributed interface that uses
Observer/Observable to notify observers of changes. 639
Exercise 22.5: Given a certain number of six-sided dice, you can calculate the theoretical probability of each
possible total. For example, with two six-sided dice, the probability of a total of seven is one in six. Write a
program that compares the theoretical distribution of sums for a given number of six-sided dice with the