1. /**
  2. * Person.java
  3. * @author Jose M Vidal <jmvidal@gmail.com>
  4. * Created on Feb 9, 2012
  5. *
  6. */
  7. 
  8. public class Person {
  9. private String name;
  10. private Person mother;
  11. private Person father;
  12. /*
  13. * True if she is a female, false if male.
  14. */
  15. private boolean female;
  16. private int yearOfBirth;
  17. public Person (String name, boolean female, int yearOfBirth){
  18. this.name = name;
  19. this.female = female;
  20. this.yearOfBirth = yearOfBirth;
  21. }
  22. public Person (String name, boolean female, int yearOfBirth, Person mother, Person father){
  23. this.name = name;
  24. this.female = female;
  25. this.yearOfBirth = yearOfBirth;
  26. this.mother = mother;
  27. this.father = father;
  28. }
  29. public String toString(){
  30. String mom = "unknown";
  31. if (mother != null)
  32. mom = mother.name;
  33. String dad = "unknown";
  34. if (father != null)
  35. dad = father.name;
  36. return name + " (" + getGender() + ") " + yearOfBirth + "\n Mother=" + mom +
  37. "\n Father=" + dad;
  38. }
  39. /**
  40. *
  41. * @param other
  42. * @return true if they share the same mom and dad
  43. */
  44. public boolean isSiblingOf(Person other){
  45. if (mother != null && father != null && other.mother != null && other.father != null)
  46. return (mother.equals(other.mother) && father.equals(other.father) && !this.equals(other));
  47. return false;
  48. }
  49. public boolean equals(Person o){
  50. return name.equals(o.name) && yearOfBirth == o.yearOfBirth;
  51. }
  52. 
  53. /**
  54. * @return the name
  55. */
  56. public String getName() {
  57. return name;
  58. }
  59. 
  60. /**
  61. * @param name the name to set
  62. */
  63. public void setName(String name) {
  64. this.name = name;
  65. }
  66. 
  67. /**
  68. * @return the mother
  69. */
  70. public Person getMother() {
  71. return mother;
  72. }
  73. 
  74. /**
  75. * @param mother the mother to set
  76. */
  77. public void setMother(Person mother) {
  78. this.mother = mother;
  79. }
  80. 
  81. /**
  82. * @return the father
  83. */
  84. public Person getFather() {
  85. return father;
  86. }
  87. 
  88. /**
  89. * @param father the father to set
  90. */
  91. public void setFather(Person father) {
  92. this.father = father;
  93. }
  94. 
  95. /**
  96. * @return the female
  97. */
  98. public boolean isFemale() {
  99. return female;
  100. }
  101. 
  102. /**
  103. * @param female the female to set
  104. */
  105. public void setFemale(boolean female) {
  106. this.female = female;
  107. }
  108. public String getGender(){
  109. if (female)
  110. return "F";
  111. else
  112. return "M";
  113. }
  114. 
  115. /**
  116. * @return the yearOfBirth
  117. */
  118. public int getYearOfBirth() {
  119. return yearOfBirth;
  120. }
  121. 
  122. /**
  123. * @param yearOfBirth the yearOfBirth to set
  124. */
  125. public void setYearOfBirth(int yearOfBirth) {
  126. this.yearOfBirth = yearOfBirth;
  127. }
  128. public Person getPaternalGrandfather(){
  129. if (father == null || father.father == null)
  130. return null;
  131. return father.father;
  132. }
  133. /**
  134. *
  135. * @param other
  136. * @return true if this person is a first cousing of other
  137. */
  138. public boolean isCousinOf(Person other){
  139. //two people are first cousins if either pair of their parents are isbilings
  140. if (father != null && other.father != null && father.isSiblingOf(other.father))
  141. return true;
  142. if (father != null && other.mother != null && father.isSiblingOf(other.mother))
  143. return true;
  144. if (mother != null && other.father != null && mother.isSiblingOf(other.father))
  145. return true;
  146. if (mother != null && other.mother != null && mother.isSiblingOf(other.mother))
  147. return true;
  148. return false;
  149. }
  150. 
  151. public static void main(String[] args) {
  152. //Part of the Weasley family tree, from http://www.hp-lexicon.org/wizards/weasley.html
  153. Person molly = new Person("Molly Weasley", true, 1950);
  154. 
  155. //Person.toString() prints out a nice view of the person
  156. System.out.println(molly); //print out molly
  157. //Implement these constructors
  158. Person arthur = new Person("Arthur Weasley", false, 1950);
  159. Person fleur = new Person("Fleur Delacour", true, 1977);
  160. Person bill = new Person("Bill Weasley", false, 1970);
  161. //Implement these setters
  162. bill.setFather(arthur);
  163. bill.setMother(molly);
  164. System.out.println(bill); //print out bill
  165. //Implement these constructors
  166. Person charlie = new Person("Charlie Weasley", false, 1972, molly, arthur);
  167. Person percy = new Person("Percy Ignatius Weasley", false, 1976, molly, arthur);
  168. Person fred = new Person("Fred Weasley", false, 1978, molly, arthur);
  169. Person george = new Person("George Weasley", false, 1978, molly, arthur);
  170. Person ron = new Person("Ronald \"Ron\" Bilius Weasley", false, 1980, molly, arthur);
  171. Person ginny = new Person("Ginerva \"Ginney\" Molly Weasley", true, 1981, molly, arthur);
  172. System.out.println(ron); //print out ron
  173. //Implement Person.isSibling(Person p) which returns true if they are siblings
  174. System.out.println("Are fred and george siblings? " + fred.isSiblingOf(george));
  175. System.out.println("Are molly and george siblings? " + molly.isSiblingOf(george));
  176. Person hermione = new Person("Hermione (Granger) Weasley", true, 1979);
  177. Person rose = new Person("Rose Wealey", true, 2006,hermione,ron);
  178. Person hugo = new Person("Hugo Wealey", true, 2008, hermione, ron);
  179. System.out.println("Rose's paternal grandpa is:\n" + rose.getPaternalGrandfather());
  180. System.out.println("Ron's paternal grandpa is:\n" + ron.getPaternalGrandfather());
  181. Person harry = new Person("Harry Potter", false, 1980);
  182. Person james = new Person("James Sirius", false, 2005, ginny, harry);
  183. Person albus = new Person("Albus Severus", false, 2006, ginny, harry);
  184. Person lily = new Person("Lily Luna", true, 2007, ginny, harry);
  185. //Implement the Person.isCousin(Person p) which returns true if they are first cousins.
  186. // X and Y are first cousins if one of their parents are siblings.
  187. System.out.println("Are rose and albus cousins? " + rose.isCousinOf(albus));
  188. System.out.println("Are albus and rose cousins? " + albus.isCousinOf(rose));
  189. System.out.println("Are harry and ron cousins? " + harry.isCousinOf(ron));
  190. System.out.println("Are lily and charlie cousins? " + lily.isCousinOf(charlie));
  191. System.out.println("Are james and albus cousins? " + james.isCousinOf(albus));
  192. System.out.println("Are james and fred cousins? " + james.isCousinOf(fred));
  193. System.out.println("Are james and hugo cousins? " + james.isCousinOf(hugo));
  194. System.out.println("Are james and rose cousins? " + james.isCousinOf(rose));
  195. 
  196. }
  197. 
  198. }