THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Exercise 11.1: Revisit the LinkedList class that you started back in Exercise 2.2 and rewrite it as a generic
class. 252


Exercise 11.2: Rewrite the Attr class from Chapter 3 as a generic class. 252


Exercise 11.3: Was Exercise 11.2 a good idea? How does Attr being generic affect the Attributed
interface defined in Chapter 4? What does it mean for Attributed objects? 252


Exercise 12.1: Create an ObjectNotFoundException class for the LinkedList class you built in
previous exercises. Add a find method that looks for an object in the list and either returns the
LinkedList object that contains the desired object or throws the exception if the object isn't found in the
list. Why is this preferable to returning null if the object isn't found? What additional data if any should
ObjectNotFoundException contain? 286


Exercise 12.2: Decide which way the following conditions should be communicated to the programmer: 295


Exercise 13.1: Write a method that counts the number of occurrences of a given character in a string. 308


Exercise 13.2: Write a method that counts the number of occurrences of a particular string in another string.
308


Exercise 13.3: As shown, the delimitedString method assumes only one such string per input string.
Write a version that will pull out all the delimited strings and return an array. 316


Exercise 13.4: Write a program to read an input string with lines of the form "type value", where type is
one of the wrapper class names (Boolean, Character, and so on) and value is a string that the type's
constructor can decode. For each such entry, create an object of that type with that value and add it to an
ArrayListsee "ArrayList" on page 582. Display the final result when all the lines have been read. Assume
a line is ended simply by the newline character '\n'. 316


Exercise 13.5: Write a method to convert strings containing decimal numbers into comma-punctuated
numbers, with a comma every third digit from the right. For example, given the string "1543729", the
method should return the string "1,543,729". 335


Exercise 13.6: Modify the method to accept parameters specifying the separator character to use and the
number of digits between separator characters. 336


Exercise 14.1: Write a program that displays the name of the thread that executes main. 341


Exercise 14.2: Modify the first version of PrintServer so that only the thread created in the constructor
can successfully execute run, using the identity of the thread as suggested. 345


Exercise 14.3: Write a class whose objects hold a current value and have a method that will add to that value,
printing the new value. Write a program that creates such an object, creates multiple threads, and invokes the
adding method repeatedly from each thread. Write the class so that no addition can be lost. 354


Exercise 14.4: Modify your code from Exercise 14.3 to use static data and methods. 354


Exercise 14.5: Modify your code from Exercise 14.4 so that threads can safely decrement the value without
using a static synchronized method. 354


Exercise 14.6: Write a program that prints the elapsed time each second from the start of execution, with
another thread that prints a message every fifteen seconds. Have the message-printing thread be notified by
the time-printing thread as each second passes by. Add another thread that prints a different message every

Free download pdf