Guest User

Untitled

a guest
Jul 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. /*
  2. * Person.java
  3. */
  4. package ex9;
  5.  
  6. /**
  7. * Test harness for Person and forefather relations.
  8. *
  9. * @author djb, mjs
  10. */
  11. public class PersonTest {
  12.  
  13. /**
  14. * tests Person by creating a tree of forefathers
  15. * and then printing out the forefathers.
  16. */
  17. public static void main(String[] args) {
  18. // create the lineage
  19. Person frank = new Person("Frank");
  20. Person earl = new Person("Earl", frank);
  21. System.out.println("frank:" + frank);
  22. System.out.println("earl:" + earl);
  23. Person dean = new Person("Dean", earl);
  24. Person carl = new Person("Carl", dean);
  25. Person benjy = new Person("Benjy", carl);
  26. Person abe = new Person("Abe", benjy);
  27.  
  28. // TESTING
  29. printForefathers(getForefathers(abe)); //tests old method
  30. printForefathers(getForefathers(frank));
  31. printForefathers(getForefathers(null));
  32. printForefathers(getForefathersRecursiveHelper(0, abe)); //boundary tests
  33. printForefathers(getForefathersRecursiveHelper(5, abe));
  34. printForefathers(getForefathersRecursiveHelper(0, null));
  35. printForefathers(getForefathersRecursive(abe)); //tests new method
  36. printForefathers(getForefathersRecursive(frank));
  37. printForefathers(getForefathersRecursive(null));
  38. }
  39.  
  40. /**
  41. * Method for printing a list of Forefathers.
  42. *
  43. * @param p the array of Person instances.
  44. */
  45. public static void printForefathers(Person[] p) {
  46. System.out.print("Forefathers: [");
  47. for (int i = 0; i < p.length; i++) {
  48. if (i > 0) {
  49. System.out.print("," + p[i]);
  50. } else {
  51. System.out.print(p[i]);
  52. }
  53. }
  54. System.out.println("]");
  55. }
  56.  
  57. /**
  58. * Calculate an array whose elements are a given
  59. * person and all her forefathers. <p>
  60. * requires: nothing
  61. * @param p person whose forefathers are to be found
  62. * @return array whose elements are <code>p</code>
  63. * (if non-null) and all his forefathers, in order
  64. * of increasing age.
  65. * (If <code>p</code> is null, the result is empty.)
  66. */
  67. public static Person[] getForefathers(Person p) {
  68.  
  69. // count the number of forefathers, including this person
  70. Person start = p;
  71. int ff = 0;
  72. while (start != null) {
  73. start = start.getFather();
  74. ff++;
  75. }
  76.  
  77. // initialise array of Forefathers
  78. Person[] forefathers = new Person[ff];
  79.  
  80. // fill in array of Forefathers
  81. Person second = p;
  82. int ins = 0;
  83. while (second != null) {
  84. forefathers[ins] = second;
  85. second = second.getFather();
  86. ins++;
  87. }
  88.  
  89. return forefathers;
  90. }
  91.  
  92. /**
  93. * auxiliary method for <code>getForefathersRecursive</code>. <p>
  94. * requires: n= 0
  95. * @param n number of initial null elements in result.
  96. * @param p Person whose forefathers are to be found.
  97. * @return array whose first n elements are null and
  98. * whose remaining elements are p (if non-null) and
  99. * all the forefathers, in order of increasing age.
  100. */
  101. private static Person[] getForefathersRecursiveHelper(int n, Person p) {
  102. Person[] arr; //creates a new array arr of type person
  103. if (p == null) { //if the person has no forefathers
  104. arr = new Person[n]; // the array arr becomes intialised with the element at index n of Person
  105. } else { //if the person has forefathers
  106. n++; //incremement n by +1
  107. arr = getForefathersRecursiveHelper(n, p.getFather()); //recursivly calls the method using the forefather of the current person as a parameter and the newly incrememnted n
  108. if (n > 0) { //if n is greater than 0
  109. arr[n - 1] = p; //store the person p in the last index of the array arr
  110. }
  111. }
  112. return arr; //return the new array with the values placed in the array correctly
  113. }
  114. /**
  115. * ensures the forefathers of the person are stored in an array using the correct recursive function
  116. * requires:
  117. * @param p of type person
  118. * @returns the array from the recursive method getForefathersRecursiveHelper
  119. */
  120. public static Person[] getForefathersRecursive(Person p) {
  121.  
  122. return getForefathersRecursiveHelper(0, p); //returns the array from the recursive method getForefathersRecursiveHelper
  123. }
  124. }
Add Comment
Please, Sign In to add comment