Given a class named Person with three public data members travel (an int ), mom
ID: 3908153 • Letter: G
Question
Given a class named Person with three public data members travel (an int), mom and dad (both references to Person objects), write a recursive instance method 'search' that recursively searches a family tree of Person objects that takes no parameters and returns a reference to a Person. You can assume a Person object appears at most once in the family tree. Return a reference to a person you find that has a travel value greater than ten thousand. Return null if no such person exists. Do not use loops.
Assume the method will belong to the following class:
JUST WRITE THE METHOD-- NOT THE WHOLE Person CLASS. (java)
Explanation / Answer
class Person { public int travel; public Person mom; /*possibly null */ public Person dad; /*possibly null */ // the search method would be defined here public Person findPerson() { if(travel > 10000) { return this; } else { Person result = null; if(mom != null) { result = mom.findPerson(); } if(dad != null) { result = dad.findPerson(); } return result; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.