Question: Exercise 7.10: This is the exercise from the book Write a method dumpClass (in java) that prints out the name of a class (including its
Exercise 7.10: This is the exercise from the book
Write a method dumpClass (in java) that prints out the name of a class (including its package name), 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 input to the method should be either the Class object that describes the class or an object of the class.
These are the requirements:
a. Class name, constructor name, field type and name (for all fields), method return type and method name (for all methods);
b. Ignore modifiers.
c. Use supplied Employee.java and Main classes.
d. The output from the program should be similar to Output7.10.txt
Below are the files used for this question:
Employee.java
/**
* Class for testing DumpClass
* @author Wei Li
*/
public class Employee {
private String name; //employee
private double salary; //yearly
Employee(String name) {this.name = name;}
public void setSalary(double amount) {salary = amount;}
public String getName() { return name;}
public double getSalary() {return salary;}
}
Main.java
public class Main {
public static void main(String[] args) throws ClassNotFoundException
{
Employee e = new Employee("John");
DumpClass dc = new DumpClass(e);
dc.dumpClass();
}
}
Output7.10.txt
null
Employee
java.lang.String name
double salary
Employee
java.lang.String getName
void setSalary
double getSalary
This question is from the book 'A Crash Course in Java'
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
