[ Team LiB ] Previous Section Next Section

9.4 Exercises

Exercise 9-1. Write a program that takes the name of a Java class as a command-line argument and uses the Class class to print out all the superclasses of that class. For example, if invoked with the argument "java.awt.Applet", the program prints the following: java.lang.Object java.awt.Component java.awt.Container java.awt.Panel.

Exercise 9-2. Modify the program you wrote in Exercise 8-1 so that it prints out all interfaces implemented by a specified class or by any of its superclasses. Check for the case of classes that implement interfaces that extend other interfaces. For example, if a class implements java.awt.LayoutManager2, the LayoutManager2 interface and LayoutManager, its superinterface, should be listed.

Exercise 9-3. Define a class named Assignment that is modeled on the Command class shown in Example 9-2. Instead of invoking a named method, as Command does, Assignment should assign a value to a named field of an object. Give your class a constructor with the following signature:

public Assignment(Object target, Field field, Object value)

Also give it an assign( ) method that, when invoked, assigns the specified value to the specified field of the specified object. Assignment should implement ActionListener and define an actionPerformed( ) method that also performs the assignment. Write a static parse( ) method for Assignment that is similar to the parse( ) method of Command. It should parse strings of the form fieldname=value and should be able to parse boolean, numeric, and string values.

    [ Team LiB ] Previous Section Next Section