Monday, February 13, 2012

Homework 5: Family Tree

In this homework you will implement a simple Person class that has the following data members:

  • String: name
  • int: yearOfBirth
  • boolean: isFemale
  • Person: mother
  • Person: father
and which implements the following methods:
  • Person(String name, boolean isFemale, int yearOfBirth): constructor
  • Person(String name, boolean isFemale, int yearOfBirth, Person mother, Person father): constructor
  • equals(Person other): returns true if this person and other have the same name and were born in the same year.
  • boolean isSiblingOf(Person other): returns true if this person and other are siblings
  • boolean isCousinOf(Person other): return true if this person and other are first cousins (at least one of their parents are siblings)
  • Person getPaternalGrandfather(): returns the paternal grandfather (dad's dad).

To test your program us the following main (cf. The Weasley Family)
public static void main(String[] args) {
        //Part of the Weasley family tree, from http://www.hp-lexicon.org/wizards/weasley.html
 Person molly = new Person("Molly Weasley", true, 1950);

 //Person.toString() prints out a nice view  of the person
 System.out.println(molly); //print out molly
  
 //Implement these constructors
 Person arthur = new Person("Arthur Weasley", false, 1950);
 Person fleur = new Person("Fleur Delacour", true, 1977);
 Person bill = new Person("Bill Weasley", false, 1970);
  
 //Implement these setters
 bill.setFather(arthur);
 bill.setMother(molly);
 System.out.println(bill); //print out bill
  
 //Implement these constructors
 Person charlie  = new Person("Charlie Weasley", false, 1972, molly, arthur);
 Person percy = new Person("Percy Ignatius Weasley", false, 1976, molly, arthur);
 Person fred = new Person("Fred Weasley", false, 1978, molly, arthur);
 Person george = new Person("George Weasley", false, 1978, molly, arthur);
 Person ron = new Person("Ronald \"Ron\" Bilius Weasley", false, 1980, molly, arthur);
 Person ginny = new Person("Ginerva \"Ginney\" Molly Weasley", true, 1981, molly, arthur);
 System.out.println(ron); //print out ron
  
 //Implement Person.isSibling(Person p) which returns true if they are siblings
 System.out.println("Are fred and george siblings? " + fred.isSiblingOf(george));
 System.out.println("Are molly and george siblings? " + molly.isSiblingOf(george));
  
 Person hermione = new Person("Hermione (Granger) Weasley", true, 1979);
 Person rose = new Person("Rose Wealey", true, 2006,hermione,ron);
 Person hugo = new Person("Hugo Wealey", true, 2008, hermione, ron);
 System.out.println("Rose's paternal grandpa is:\n" + rose.getPaternalGrandfather());
 System.out.println("Ron's paternal grandpa is:\n" + ron.getPaternalGrandfather());
 Person harry = new Person("Harry Potter", false, 1980);
 Person james = new Person("James Sirius", false, 2005, ginny, harry);
 Person albus = new Person("Albus Severus", false, 2006, ginny, harry);
 Person lily = new Person("Lily Luna", true, 2007, ginny, harry);
  
 //Implement the Person.isCousin(Person p) which returns true if they are first cousins.
 //  X and Y are first cousins if one of their parents are siblings.
 System.out.println("Are rose and albus cousins? " + rose.isCousinOf(albus));
 System.out.println("Are albus and rose cousins? " + albus.isCousinOf(rose));
 System.out.println("Are harry and ron cousins? " + harry.isCousinOf(ron));
 System.out.println("Are lily and charlie cousins? " + lily.isCousinOf(charlie));
 System.out.println("Are james and albus cousins? " + james.isCousinOf(albus));
 System.out.println("Are james and fred cousins? " + james.isCousinOf(fred));
 System.out.println("Are james and hugo cousins? " + james.isCousinOf(hugo));
 System.out.println("Are james and rose cousins? " + james.isCousinOf(rose));

 }

When you run that main it should print out:
Molly Weasley (F) 1950
    Mother=unknown
    Father=unknown
Bill Weasley (M) 1970
    Mother=Molly Weasley
    Father=Arthur Weasley
Ronald "Ron" Bilius Weasley (M) 1980
    Mother=Molly Weasley
    Father=Arthur Weasley
Are fred and george siblings? true
Are molly and george siblings? false
Rose's paternal grandpa is:
Arthur Weasley (M) 1950
    Mother=unknown
    Father=unknown
Ron's paternal grandpa is:
null
Are rose and albus cousins? true
Are albus and rose cousins? true
Are harry and ron cousins? false
Are lily and charlie cousins? false
Are james and albus cousins? false
Are james and fred cousins? false
Are james and hugo cousins? true
Are james and rose cousins? true

This homework is due Monday, February 20 @noon in the dropbox.cse.sc.edu.

2 comments:

jmvidal said...

Over email many students have asked me a similar question, and the answer is always "because mother is null". Remember that if an object has a data member that is also an object (like, mother) and you do NOT set it to anything (that is, you don't say "mother = new Person()") then that property will be set to null by default (mother == null).

jmvidal said...

As one of you pointed out, equals was wrong as stated. I have now fixed it. Its signature is 'boolean equals(Person other)'.