THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

actual results over a large number of "rolls" using Random to generate numbers between one and six. Does it
matter which of the number-generating methods you use? 641


Exercise 22.6: Write a program that tests nextGaussian, displaying the results of a large number of runs
as a graph (a bar chart of * characters will do). 641


Exercise 22.7: Rewrite readCSVTable so that the number of cells of data expected is passed as an
argument. 646


Exercise 22.8: As it stands, readCSVTable is both too strict and too lenient on the input format it expects.
It is too strict because an empty line at the end of the input will cause the IOException to be thrown. It is
too lenient because a line of input with more than three commas will not cause an exception. Rectify both of
these problems. 646


Exercise 22.9: Referring back to the discussion of efficiency of regular expressions on page 329, devise at
least four patterns that will parse a line of comma-separated-values. (Hint: In addition to the suggestion on
page 329 also consider the use of greedy versus non-greedy quantifiers.) Write a benchmark program that
compares the efficiency of each pattern, and be sure that you test with both short strings between commas and
very long strings. 647


Exercise 22.10: Write a method to tokenize input that ignores comments, using the comment pattern as part of
the scanner's delimiter. 650


Exercise 22.11: Write a version of readCSV that uses a StreamTokenizer rather than a Scanner. 650


Exercise 22.12: Write a version of the attribute reading method from page 533 that uses a Scanner. For this
exercise it is only necessary that both versions accept the same correctly formatted input. 650


Exercise 22.13: Extend your solution to Exercise 22.12 so that misplaced = characters are detected, as in the
StreamTokenizer version. (Hint: You might want to try to dynamically change the delimiter pattern
between certain tokens.) 650


Exercise 22.14: Write a method that will take a string containing floating-point numbers, break it up using
whitespace as the delimiter, and return the sum of the numbers. 653


Exercise 22.15: Write a calculator that has all Math or StrictMath functions, as well as (at least) the basic
operators +, -, *, /, and %. (The simplest form is probably a reverse Polish stack calculator because operator
precedence is not an issue.) 659


Exercise 23.1: Write the plugTogether method. You will need threads. 669


Exercise 23.2: Write a program that runs exec on its command-line arguments and prints the output from the
command, preceding each line of output by its line number. 672


Exercise 23.3: Write a program that runs exec on command-line arguments and prints the output from the
command, killing the command when a particular string appears in the output. 672


Exercise 24.1: Get GlobalHello to work with the example locales. Add some more locales, using
ListResourceBundle, .properties files, and your own specific subclass of ResourceBundle.
693


Exercise 24.2: Select six different locales and six different currencies, and print a table showing the currency
symbol for each currency in each locale. 694

Free download pdf