THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Exercise 17.1: Write a program to examine the amount of memory available on start up and after allocation of
a number of objects. Try invoking the garbage collector explicitly to see how the amount of free memory
changesmake sure you don't hold references to the newly allocated objects of course. 454


Exercise 17.2: Modify DataHandler so that lastFile is also stored weakly. 458


Exercise 17.3: Rework the resource implementation class so that it uses a reference object to keep track of the
key instead of using the hash code. 464


Exercise 17.4: Modify the reaper thread so that it stays alive after shutdown until all the allocated resources
can be released. 464


Exercise 17.5: Redesign the resource manager to not use a reaper thread. Be clear on what semantics the
resource manager has and on when resources will be released. 464


Exercise 19.1: Add doc comments to your LinkedList class from Exercise 2.16. Generate the javadoc and
ask someone else to write a simple program using your class. Repeat, improving your doc comments if
needed, until someone can do so. 496


Exercise 19.2: Expand on Exercise 19.1 by including the private members. Generate the full (private members
included) javadoc and ask someone else to explain the class to you. Repeat, improving your comments if
needed, until someone can do so. 496


Exercise 20.1: Rewrite the translateByte program as a method that translates the contents of an
InputStream onto an OutputStream, in which the mapping and the streams are parameters. For each
type of InputStream and OutputStream you read about in this chapter, write a new main method that
uses the translation method to operate on a stream of that type. If you have paired input and output streams,
you can cover both in one main method. 506


Exercise 20.2: Rewrite the TRanslateByte class as a filter. 518


Exercise 20.3: Create a pair of Filter stream classes that encrypt bytes using any algorithm you choosesuch
as XORing the bytes with some valuewith your DecryptInputStream able to decrypt the bytes that your
EncryptOutputStream class creates. 518


Exercise 20.4: Create a subclass of FilterReader that will return one line of input at a time via a method
that blocks until a full line of input is available. 518


Exercise 20.5: Write a program that reads a specified file and searches for a specified word, printing each line
number and line in which the word is found. 528


Exercise 20.6: Write a program that takes input of the form name op value, where name is one of three
words of your choosing, op is +, -, or =, and value is a number. Apply each operator to the named value.
When input is exhausted, print the three values. For extra credit, use the HashMap class that was used for
AttributedImpl so that you can use an arbitrary number of named values. 536


Exercise 20.7: Add a method to the Attr class of Chapter 3 that writes the contents of an object to a
DataOutputStream and add a constructor that will read the state from a DataInputStream. 539


Exercise 20.8: Write a program that reads a file with entries separated by lines starting with %% and creates a
table file with the starting position of each such entry. Then write a program that uses that table to print a
random entry (see the Math.random method described in "Math and StrictMath" on page 657). 543


Exercise 20.9: Write a method that, given one or more pathnames, will print all the information available

Free download pdf