Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Person.java
- */
- package ex9;
- /**
- * Test harness for Person and forefather relations.
- *
- * @author djb, mjs
- */
- public class PersonTest {
- /**
- * tests Person by creating a tree of forefathers
- * and then printing out the forefathers.
- */
- public static void main(String[] args) {
- // create the lineage
- Person frank = new Person("Frank");
- Person earl = new Person("Earl", frank);
- System.out.println("frank:" + frank);
- System.out.println("earl:" + earl);
- Person dean = new Person("Dean", earl);
- Person carl = new Person("Carl", dean);
- Person benjy = new Person("Benjy", carl);
- Person abe = new Person("Abe", benjy);
- // TESTING
- printForefathers(getForefathers(abe)); //tests old method
- printForefathers(getForefathers(frank));
- printForefathers(getForefathers(null));
- printForefathers(getForefathersRecursiveHelper(0, abe)); //boundary tests
- printForefathers(getForefathersRecursiveHelper(5, abe));
- printForefathers(getForefathersRecursiveHelper(0, null));
- printForefathers(getForefathersRecursive(abe)); //tests new method
- printForefathers(getForefathersRecursive(frank));
- printForefathers(getForefathersRecursive(null));
- }
- /**
- * Method for printing a list of Forefathers.
- *
- * @param p the array of Person instances.
- */
- public static void printForefathers(Person[] p) {
- System.out.print("Forefathers: [");
- for (int i = 0; i < p.length; i++) {
- if (i > 0) {
- System.out.print("," + p[i]);
- } else {
- System.out.print(p[i]);
- }
- }
- System.out.println("]");
- }
- /**
- * Calculate an array whose elements are a given
- * person and all her forefathers. <p>
- * requires: nothing
- * @param p person whose forefathers are to be found
- * @return array whose elements are <code>p</code>
- * (if non-null) and all his forefathers, in order
- * of increasing age.
- * (If <code>p</code> is null, the result is empty.)
- */
- public static Person[] getForefathers(Person p) {
- // count the number of forefathers, including this person
- Person start = p;
- int ff = 0;
- while (start != null) {
- start = start.getFather();
- ff++;
- }
- // initialise array of Forefathers
- Person[] forefathers = new Person[ff];
- // fill in array of Forefathers
- Person second = p;
- int ins = 0;
- while (second != null) {
- forefathers[ins] = second;
- second = second.getFather();
- ins++;
- }
- return forefathers;
- }
- /**
- * auxiliary method for <code>getForefathersRecursive</code>. <p>
- * requires: n= 0
- * @param n number of initial null elements in result.
- * @param p Person whose forefathers are to be found.
- * @return array whose first n elements are null and
- * whose remaining elements are p (if non-null) and
- * all the forefathers, in order of increasing age.
- */
- private static Person[] getForefathersRecursiveHelper(int n, Person p) {
- Person[] arr; //creates a new array arr of type person
- if (p == null) { //if the person has no forefathers
- arr = new Person[n]; // the array arr becomes intialised with the element at index n of Person
- } else { //if the person has forefathers
- n++; //incremement n by +1
- arr = getForefathersRecursiveHelper(n, p.getFather()); //recursivly calls the method using the forefather of the current person as a parameter and the newly incrememnted n
- if (n > 0) { //if n is greater than 0
- arr[n - 1] = p; //store the person p in the last index of the array arr
- }
- }
- return arr; //return the new array with the values placed in the array correctly
- }
- /**
- * ensures the forefathers of the person are stored in an array using the correct recursive function
- * requires:
- * @param p of type person
- * @returns the array from the recursive method getForefathersRecursiveHelper
- */
- public static Person[] getForefathersRecursive(Person p) {
- return getForefathersRecursiveHelper(0, p); //returns the array from the recursive method getForefathersRecursiveHelper
- }
- }
Add Comment
Please, Sign In to add comment