0% found this document useful (0 votes)
45 views8 pages

Object Oriented Programming

The document defines classes for representing people, families, and relationships between family members in a genealogy system. The Person class stores attributes like name and gender and defines methods for relationships like marriage and having children. The Family class represents a married couple and their children. The Test class creates sample data and tests methods like finding siblings, cousins, and other family relationships.

Uploaded by

runo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Object Oriented Programming

The document defines classes for representing people, families, and relationships between family members in a genealogy system. The Person class stores attributes like name and gender and defines methods for relationships like marriage and having children. The Family class represents a married couple and their children. The Test class creates sample data and tests methods like finding siblings, cousins, and other family relationships.

Uploaded by

runo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

package p2;

public class Person {


public String name;
public int gender;
public Person mother,father;
public Family family;
public int i=0;
public Person [] children;
int count =0;

//

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;
}

public Family married(Person p1){


if (this.gender==0 )
return new Family(p1,this);

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;
}

public Person[] allBrothers(){


Person brothers[] = new Person[0];
Person children[] = this.mother.family.children;
if(children != null)
{
for(int i=0; i<this.mother.family.children.length; i++)
{
if(this != children[i] && children[i].gender == 0)
{
brothers = addPersonb(children[i], brothers);
}
}
if(brothers.length == 0)
return null;
return brothers;
}else
return null;
}
public Person[] allSisters(){
Person sisters[] = new Person[0];

Person children[] = this.mother.family.children;


if(children != null)
{
for(int i=0; i<this.mother.family.children.length; i++)
{
if(this != children[i] && children[i].gender == 1)
{
sisters = addPersonb(children[i], sisters);
}
}
if(sisters.length == 0)
return null;
return sisters;
}else
return null;
}

public Person[] allSiblings(){


Person temp[]= new Person[0];
if(children!=null){
for(int i=0;i<this.mother.family.children.length;i++){
if(this.father.children[i]!= this){
temp[i]=this.mother.family.children[i];
}
else
temp[i]=this.mother.family.children[i+1];
}
return temp;
}else return null;
}
public Person[] allCousins(){
Person [] siblingsOfMother = this.mother.allSiblings();
Person [] siblingsOfFather = this.father.allSiblings();
Person [] cousins = new Person[0];
for(int i = 0; i<siblingsOfMother.length; i++)
{
for(int j=0; j<siblingsOfMother[i].children.length; j++)
{
cousins =
addPersonb(siblingsOfMother[i].children[j], cousins);
}
}
for(int i = 0; i<siblingsOfFather.length; i++)
{
for(int j=0; j<siblingsOfFather[i].children.length; j++)
{

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;
}

public boolean areBrothers(Person p){


if (this.mother.family==p.mother.family)
return true;
else return false;
}
public boolean areCousins(Person p){
if (this.father.mother==p.father.mother ||
this.father.mother==p.mother.mother
|| this.mother.mother==p.father.mother ||
this.mother.mother==p.mother.mother)
return true;
else return false;
}
}

package p2;

public class Family {

public Person husband;


public Person wife;
public Person children[];

public Family(Person wife,Person husband){


this.husband=husband;
this.wife=wife;
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;
}

package p2;

public class Test {

public static void main(String[] args) {

Person p1 = new Person("Marilyn", 1, null, null);


Person p2 = new Person("John", 0, null, null);
Person p3 = new Person("Hannah", 1, null, null);
Person p4 = new Person("Brad", 0, null, null);

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);

System.out.println("Jane's sisters are:"+


p1.children[0].allSisters());

System.out.println("Mac's siblings are:"+


p3.children[2].allSiblings());

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("Mother in law of "+ p1.children[1].name +


":"+ p1.getMotherInLaw());

System.out.println("Grandparents of
John:"+p1.children[1].children[0].grandparents());

}
}

You might also like