import tester.Tester; // to represent an ancestor tree interface IAT{ // count how many persons does this tree represent public int countPersons(); // is there a person with the given name in this ancestor tree? public boolean containsPerson(String aname); // compute the oldest generation known in this ancestor tree public int oldestGen(); } // to represent an unknown member of an ancestor tree class Unknown implements IAT{ Unknown() {} // count how many persons does this empty ancestor tree represent public int countPersons(){ return 0; } // is there a person with the given name in this ancestor tree? public boolean containsPerson(String aname){ return false; } // compute the oldest generation known in this ancestor tree public int oldestGen(){ return 0; } } // to represent a person with the person's ancestor tree class Person implements IAT{ String name; IAT mom; IAT dad; Person(String name, IAT mom, IAT dad){ this.name = name; this.mom = mom;this.dad = dad; } /* TEMPLATE FIELDS: ...this.name... -- String ...this.mom... -- IAT ...this.dad... -- IAT METHODS: ...this.countPersosn()... -- int ...this.containsPerson(String)... -- boolean ...this.oldestGen()... -- int METHODS FOR FIELDS: ...this.mom.countPersons()... -- int ...this.dad.countPersons()... -- int ...this.mom.containsPerson(String) -- boolean ...this.dad.containsPerson(String) -- boolean ...this.mom.oldestGen()... -- int ...this.dad.oldestGen()... -- int */ // count how many persons does this person's tree represent (including self) public int countPersons(){ return this.mom.countPersons() + this.dad.countPersons() + 1; } // is there a person with the given name in this ancestor tree? public boolean containsPerson(String aname){ return this.name.equals(aname) || this.mom.containsPerson(aname) || this.dad.containsPerson(aname); } // compute the oldest generation known in this ancestor tree public int oldestGen(){ return 1 + this.max(this.mom.oldestGen(), this.dad.oldestGen()); } // helper to compute max of two integers int max(int n1, int n2){ if (n1 >= n2) return n1; else return n2; } } // examples and tests for the classes that represent Ancestors class ExamplesAncestors{ IAT unknown = new Unknown(); IAT ann = new Person("Ann", this.unknown, this.unknown); IAT tim = new Person("Tim", this.unknown, this.unknown); IAT mary = new Person("Mary", this.ann, this.tim); IAT robert = new Person("Robert", this.unknown, this.unknown); IAT john = new Person("John", this.unknown, this.unknown); IAT jane = new Person("Jane", this.mary, this.robert); IAT dan = new Person("Dan", this.jane, this.john); // test the method countPersons for IAT classes boolean testCountPersons(Tester t){ return t.checkExpect(this.unknown.countPersons(), 0) && t.checkExpect(this.ann.countPersons(), 1) && t.checkExpect(this.jane.countPersons(), 5) && t.checkExpect(this.dan.countPersons(), 7); } // test the method containsPerson for IAT classes boolean testContainsPerson(Tester t){ return t.checkExpect(this.unknown.containsPerson("Pete"), false) && t.checkExpect(this.ann.containsPerson("Pete"), false) && t.checkExpect(this.ann.containsPerson("Ann"), true) && t.checkExpect(this.jane.containsPerson("Tim"), true) && t.checkExpect(this.jane.containsPerson("Dan"), false) && t.checkExpect(this.dan.containsPerson("John"), true); } // test the method oldestGen for IAT classes boolean testOldestGen(Tester t){ return t.checkExpect(this.unknown.oldestGen(), 0) && t.checkExpect(this.ann.oldestGen(), 1) && t.checkExpect(this.jane.oldestGen(), 3) && t.checkExpect(this.dan.oldestGen(), 4); } // we need an object that can only be Person - not IAT // otherwise, Java cannot guarantee there will be the method 'max' Person zz = new Person("zz", unknown, unknown); // tests for the max helper boolean testMax(Tester t){ return t.checkExpect(this.zz.max(5, 7), 7) && t.checkExpect(this.zz.max(15, 7), 15); } }