THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

seven seconds without modifying the time-printing thread. 358


Exercise 14.7: Run Babble multiple times and examine the output: Is it always the same? If possible, run it
on different systems and compare. 362


Exercise 14.8: Experiment with the Friendly program. How often does the deadlock actually happen on
your system? If you add yield calls, can you change the likelihood of deadlock? If you can, try this exercise
on more than one kind of system. Remove the deadlock potential without getting rid of the synchronization.
364


Exercise 14.9: Write a method that takes a thread group and starts a thread that periodically prints the
hierarchy of threads and thread groups within that group, using the methods just described. Test it with a
program that creates several short-lived threads in various groups. 379


Exercise 16.1: Modify TypeDesc to skip printing anything for the Object class. It is redundant because
everything ultimately extends it. Use the reference for the Class object for the Object type. 407


Exercise 16.2: Modify TypeDesc to show whether the named type is a nested type, and if so, what other
type it is nested within. 408


Exercise 16.3: Modify ClassContents to show information for all declared and all public inherited
members. Make sure you don't list anything twice. 411


Exercise 16.4: Write a program that prints all the available annotations applied to a given type. (Only
annotations with a retention policy of RUNTIME will be available.) 415


Exercise 16.5: Expand ClassContents to include the available annotation information for each member.
416


Exercise 16.6: Create an Interpret program that creates an object of a requested type and allows the user
to examine and modify fields of that object. 420


Exercise 16.7: Modify your Interpret program to invoke methods on the object. You should properly
display any values returned or exceptions thrown. 422


Exercise 16.8: Modify your Interpret program further to let users invoke constructors of an arbitrary
class, displaying any exceptions. If a construction is successful, let users invoke methods on the returned
object. 425


Exercise 16.9: Use reflection to write a program that will print a full declaration of a named class, including
everything except the import statements, comments, and code for initializers, constructors, and methods. The
member declarations should appear just as you would write them. You will need to use all the reflection
classes you have seen. Also note that the toString methods of many of the reflection objects will not
provide the information you want in the correct format, so you will need to piece together the individual
pieces of information. 429


Exercise 16.10: Modify Interpret further to allow users to specify a type and size of array to create; set
and get the elements of that array; and access fields and invoke methods on specific elements of the array. 432


Exercise 16.11: Expand on Game and Player to implement a simple game, such as tic-tac-toe. Score some
Player implementations over several runs each. 441


Exercise 16.12: Modify your results for Exercise 16.11 to allow player strategies to use attached resources by
implementing findResource and findResources. 444

Free download pdf