Object Oriented Programming
Object Oriented Programming
//
public Person (String name, int gender, Person mother, Person father) {
this.name=name;
this.gender=gender;
this.mother=mother;
this.father=father;
children = new Person[0];
}
public void addPerson(Person p){
Person [] temp=new Person[children.length+1];
for(int i=0;i<children.length;i++){
temp[i]=children[i];
}
temp[children.length]=p;
children=temp;
}
public Person[] addPersonb(Person p, Person [] ar)
{
Person [] temp = new Person[ar.length+1];
for(int i = 0; i<ar.length; i++)
{
temp[i]= ar[i];
}
temp[ar.length] = p;
return temp;
}
else
return new Family(this,p1);
}
public Person born(String name,int gender,Person father){
Person c=new Person(name,gender,this,father);
this.addPerson(c);
father.addPerson(c);
this.family.addPerson(c);
return c;
}
public Person[] allChilderen(){
if(this.children.length == 0)
return null;
return this.children;
}
public Person getMotherInLaw(){
if(this.family == null)
return null;
if (this.gender==0)
return this.family.wife.mother;
else
return this.family.husband.mother;
}
cousins =
addPersonb(siblingsOfFather[i].children[j], cousins);
}
}
if(cousins.length == 0)
return null;
return cousins;
}
public Person[] grandparents(){
Person []a = new Person [3];
a[0]=this.mother.mother;
a[1]=this.mother.father;
a[2]=this.father.mother;
a[3]=this.father.father;
return a;
}
package p2;
package p2;
p1.married(p2);
p3.married(p4);
p1.born("Jane", 1, p2);
p1.born("Marie", 1, p2);
p1.born("Tom", 0, p2);
p3.born("Mimi-Rose", 1, p4);
p3.born("Gabe", 0, p4);
p3.born("Mac", 0, p4);
if (p3.children[1].areBrothers(p3.children[2])){
System.out.println("Mac and Gabe are brothers");}
p1.children[1].married(p3.children[2]);
p1.children[1].born("John", 0, p3.children[2]);
System.out.println("Grandparents of
John:"+p1.children[1].children[0].grandparents());
}
}