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
, theLayoutManager2
interface andLayoutManager
, its superinterface, should be listed.Exercise 9-3. Define a class named
Assignment
that is modeled on theCommand
class shown in Example 9-2. Instead of invoking a named method, asCommand
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 implementActionListener
and define anactionPerformed( )
method that also performs the assignment. Write a staticparse( )
method forAssignment
that is similar to theparse( )
method ofCommand
. It should parse strings of the formfieldname
=
value
and should be able to parse boolean, ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.