Write a Java program consisting of a method called dumpClass and a main method w
ID: 3814451 • Letter: W
Question
Write a Java program consisting of a method called dumpClass and a main method with which to test it. The dumpClass method should print out:
1) the name of a class (including its package name),
2) its superclass, and all of its
constructors,
methods, and
fields, including parameter and field types and modifiers (such as static and final).
Format the output to look as much as possible like a class definition. The argument passed to the method should be either the Class object that describes the class or an object of the class type.
Explanation / Answer
/** * @fileName Test.java * @author ravi * @since 7/3/17 */ package dumpclass; public class Dump extends DumpSuper { public Dump() { } public static void main(String[] args) throws NoSuchMethodException { Dump dump = new Dump(); dump.dumpClass(dump); } public void dumpClass(Dump dump) throws NoSuchMethodException { try { System.out.println(Class.forName("dumpclass.Dump").getName()); System.out.println(Class.forName("dumpclass.Dump").getPackage()); System.out.println(Class.forName("dumpclass.Dump").getSuperclass()); System.out.println(Class.forName("dumpclass.Dump").getDeclaredField()); System.out.println(Class.forName("dumpclass.Dump").getConstructor()); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(super.toString()); } } class DumpSuper { }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.