Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Determines the inheritance tree for a given class
- *
- * @author Thomas Lee <tlee08@my.heartland.edu>
- *
- *
- *
- */
- import java.util.Scanner;
- public class Assignment2{
- public static void main (String[] args) throws ClassNotFoundException{
- System.out.println("Please enter a Java class to show the inheritance hierarchy for (ex - java.lang.Float): ");
- Scanner scan = new Scanner (System.in);
- String className = scan.nextLine();
- try{
- Class myClass = Class.forName(className);
- TopDownR(myClass);
- System.out.print("\n");
- BottomUpR(myClass);
- System.out.print("\n");
- TopDownL(myClass);
- System.out.print("\n");
- BottomUpL(myClass);
- }catch (Exception e){
- System.out.println("Invalid class name or no Superclass.");
- }
- }
- public static void TopDownR(Class myClass){
- if(myClass.getName().equals("java.lang.Object")){
- System.out.print("\n");
- }else{
- TopDownR(myClass.getSuperclass());
- System.out.print("The class " + myClass.getSuperclass().getName()
- + " is the superclass of the " + myClass + "\n");
- }
- }
- public static void BottomUpR(Class myClass){
- if(myClass.getName().equals("java.lang.Object")){
- System.out.print("");
- }else{
- System.out.print("The " + myClass + " is a subclass of the class "
- + myClass.getSuperclass().getName() + "\n");
- BottomUpR(myClass.getSuperclass());
- }
- }
- //Problem method here
- public static void TopDownL(Class myClass){
- String[] classTree = new String[10];
- int i = 0;
- while(!(myClass.getName().equals("java.lang.Object"))){
- classTree[i] = ("The class " + myClass.getSuperclass().getName()
- + " is the superclass of the " + myClass + "\n");
- System.out.print(classTree[i]);
- i++;
- myClass = myClass.getSuperclass();
- }
- for(i = classTree.length; i>=0; i--){
- System.out.print(classTree[i]);
- }
- }
- //Loop works here
- public static void BottomUpL(Class myClass){
- while(!(myClass.getName().equals("java.lang.Object"))){
- System.out.print("The " + myClass + " is a subclass of the class "
- + myClass.getSuperclass().getName() + "\n");
- myClass = myClass.getSuperclass();
- }
- }
- }
Add Comment
Please, Sign In to add comment