Guest User

Untitled

a guest
Jan 22nd, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. /**
  2. * Determines the inheritance tree for a given class
  3. *
  4. * @author Thomas Lee <tlee08@my.heartland.edu>
  5. *
  6. *
  7. *
  8. */
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13.  
  14. public class Assignment2{
  15.  
  16.  
  17.  
  18. public static void main (String[] args) throws ClassNotFoundException{
  19.  
  20. System.out.println("Please enter a Java class to show the inheritance hierarchy for (ex - java.lang.Float): ");
  21.  
  22. Scanner scan = new Scanner (System.in);
  23.  
  24. String className = scan.nextLine();
  25.  
  26. try{
  27.  
  28. Class myClass = Class.forName(className);
  29.  
  30. TopDownR(myClass);
  31. System.out.print("\n");
  32. BottomUpR(myClass);
  33. System.out.print("\n");
  34. TopDownL(myClass);
  35. System.out.print("\n");
  36. BottomUpL(myClass);
  37.  
  38. }catch (Exception e){
  39.  
  40. System.out.println("Invalid class name or no Superclass.");
  41.  
  42. }
  43.  
  44. }
  45.  
  46.  
  47.  
  48. public static void TopDownR(Class myClass){
  49.  
  50. if(myClass.getName().equals("java.lang.Object")){
  51.  
  52. System.out.print("\n");
  53.  
  54. }else{
  55.  
  56. TopDownR(myClass.getSuperclass());
  57. System.out.print("The class " + myClass.getSuperclass().getName()
  58.  
  59. + " is the superclass of the " + myClass + "\n");
  60.  
  61. }
  62.  
  63. }
  64.  
  65. public static void BottomUpR(Class myClass){
  66. if(myClass.getName().equals("java.lang.Object")){
  67. System.out.print("");
  68. }else{
  69. System.out.print("The " + myClass + " is a subclass of the class "
  70. + myClass.getSuperclass().getName() + "\n");
  71. BottomUpR(myClass.getSuperclass());
  72. }
  73.  
  74. }
  75. //Problem method here
  76. public static void TopDownL(Class myClass){
  77. String[] classTree = new String[10];
  78. int i = 0;
  79. while(!(myClass.getName().equals("java.lang.Object"))){
  80. classTree[i] = ("The class " + myClass.getSuperclass().getName()
  81.  
  82. + " is the superclass of the " + myClass + "\n");
  83. System.out.print(classTree[i]);
  84. i++;
  85. myClass = myClass.getSuperclass();
  86. }
  87. for(i = classTree.length; i>=0; i--){
  88. System.out.print(classTree[i]);
  89. }
  90. }
  91. //Loop works here
  92. public static void BottomUpL(Class myClass){
  93. while(!(myClass.getName().equals("java.lang.Object"))){
  94. System.out.print("The " + myClass + " is a subclass of the class "
  95. + myClass.getSuperclass().getName() + "\n");
  96. myClass = myClass.getSuperclass();
  97. }
  98. }
  99. }
Add Comment
Please, Sign In to add comment