Question
package comp1110.exam; import java.util.List; /** * COMP1110 Exam, Question 1.2 */ public class Q1FamilyTree { /** * This class represents an individual with zero or
package comp1110.exam; import java.util.List; /** * COMP1110 Exam, Question 1.2 */ public class Q1FamilyTree { /** * This class represents an individual with zero or more children. */ static class Individual { public String name; /** * This individual's immediate descendants. * If this individual has no children, this field is null. */ public Individual[] children; public Individual(String name, Individual[] children) { this.name = name; this.children = children; } } /** * This function accepts an Individualancestor
representing * the root (common ancestor) of a family tree * and the name of a target individual to find within that family tree, * and returns a string representation of all the ancestors of that * individual, each separated by the string " born of ". ** If target name matches the name of
ancestor
, then only * the target name is returned. ** If the target name is not found within the family tree descended from *
ancestor
, then null is returned. ** For example, given an Individual homer representing a person named * "Homer", who has children named "Lisa" and "Bart": * getAncestry(homer, "Lisa") returns "Lisa born of Homer"; * getAncestry(homer, "Bart") returns "Bart born of Homer"; and * getAncestry(homer, "Homer") returns "Homer"; and * getAncestry(homer, "Barney") returns null *
* Note: each individual has only one parent in the family tree. * * @param ancestor the root (common ancestor) of a family tree * @param targetName the name of an individual to find in the family tree * @return a String representing the ancestry of the individual named *
targetName
, or null if no such individual is found */ public static String getAncestry(Individual ancestor, String targetName) { // FIXME complete this method return ""; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started