Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Homework 13 Family Database objectives: Practice ArrayList A ab, e. You are goin

ID: 3842337 • Letter: H

Question

Homework 13 Family Database objectives: Practice ArrayList A ab, e. You are going to write a set ofsupporting classes for people to query their matemal line, patemal line and children by building a family database. A family database contains genealogical data for family history. You are to implement two classes. You should implement a class called Person that represents a person and stores references to the person's mother, father, and any children the person has. It should have the following public constructor and methods: Person (String personName) o Constructor that takes a person's name as an argument. String getName() o Returns the name of this person. Person get Mother o Returns a Person object that represents this person's mother. Person get Father o Returns a Person object that represents this person's father. int num Kids o Returns the number of children of this person. Person nth Kid (int n) o Returns the nth child of this person (n 0, 1, 2, 3, Notice that n starts from 0 which means the first child and you should handle the case of n being out of bound. void set (Person mother) Mother o Sets the mother of this person. void set Father (Person father o Sets the father of this person. void add Kid Person kid) o Adds the given kid as a child of this person. You should also implement a class called FamilyInfo that store the overall list of Persons in a family history. It should have the following public constructor and method Family Info 0 o Constructor that creates an empty family database. Person get Person (string name) o Returns a reference to the Person with the given name if existing in the database returns null if not found. void read (Scanner input)

Explanation / Answer

/**
*
*/
package family;

import java.util.ArrayList;

/**
* @author sanjay k
*
*/
public class Person {

   private String name;
   private Person mother;
   private Person father;
   private int numKids;
   private ArrayList<Person> kids;
   /**
   *
   */
   public Person(String name) {
       this.name = name;
       kids = new ArrayList<Person>();
   }
  
   public String getName(){
       return this.name;
   }
  
   public Person getMother(){
       return this.mother;
   }
  
   public Person getFather(){
       return this.father;
   }
  
   public int numKids(){
       return this.numKids;
   }
  
   public Person nthKid(int n){
       if(n>=numKids){
           return null;
       }
       else{
           return kids.get(n);
       }
   }
  
   public void setMother(Person mother){
       this.mother = mother;
   }
  
   public void setFather(Person father){
       this.father = father;
   }
  
   public void addKid(Person kid){
       kids.add(kid);
       numKids++;
   }
}

package family;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class FamilyInfo {

   private ArrayList<Person> history;
  
   public FamilyInfo() {
       history = new ArrayList<Person>();
   }
  
   //retrieve person object using name
   public Person getPerson(String name){
      
       Person p;
       for(int i=0;i<history.size();i++){
           p = history.get(i);
           if(p.getName().equals(name)){
               return p;
           }
       }
       return null;
   }
  
   public void read(Scanner input){
      
       System.out.println("What is the input file?");
       String fileName = input.nextLine();
       File file = new File(fileName);

   try {

   Scanner sc = new Scanner(file);
   Person p;
   //addding all the perons to the history
   while (sc.hasNextLine()) {
   String name = sc.nextLine().trim();
   if(name.equalsIgnoreCase("END")){
       break;
   }
   p = new Person(name);
   history.add(p);
   }
  
   while (sc.hasNextLine()) {
   String name = sc.nextLine().trim();
   Person person = getPerson(name);
   Person mother = null;
   Person father = null;
   String motherName = "";
   String fatherName = "";
   if(sc.hasNextLine()){
       //kid -> mother relation
       motherName = sc.nextLine().trim();
       if(!motherName.equalsIgnoreCase("unknown")){
           mother = getPerson(motherName);
           //System.out.println(person.getName()+"->setting mother"+mother.getName());
           person.setMother(mother);
           mother.addKid(person);
       }
   }
   if(sc.hasNextLine()){
       //kid -> dfather relation
       fatherName = sc.nextLine().trim();
       if(!fatherName.equalsIgnoreCase("unknown")){
           father = getPerson(fatherName);
           //System.out.println(person.getName()+"->setting father"+father.getName());
           person.setFather(father);
           father.addKid(person);
       }
   }
   if(motherName.equalsIgnoreCase("unknown") && fatherName.equalsIgnoreCase("unknown")){
       person.setFather(null);
       person.setMother(null);
   }
   }
   sc.close();
   }
   catch(FileNotFoundException e) {
   System.out.println("Input file not found!!");
   }
   }
}

package family;

import java.util.Scanner;

public class H13_kai {

   public static void main(String[] args) {
      
       FamilyInfo family = new FamilyInfo();
       Scanner sc = new Scanner(System.in);
       family.read(sc);
      
       System.out.println("What is the Person Name?");
       Person p = family.getPerson(sc.nextLine());
       Person temp = p;
      
       System.out.println("Maternal line:");
       int count = 0;
       //printing mother hierarchy
       while(temp.getMother()!=null){
           for(int i=0;i<count;i++){
               System.out.print(" ");
           }
           //System.out.println("mother name:");
           System.out.println(temp.getMother().getName());
           count++;
           temp = temp.getMother();
       }
      
      
       System.out.println("Paternal line:");
       count = 0;
       temp = p;
       //priting father hierarchy
       while(temp.getFather()!=null){
           for(int i=0;i<count;i++){
               System.out.print(" ");
           }
           System.out.println(temp.getFather().getName());
           count++;
           temp = temp.getFather();
       }
       //priting childresn
       System.out.println("Children:");
       for(int i=0;i<p.numKids();i++){
           System.out.println(p.nthKid(i).getName());
       }
   }

}

input.data

personA
personB
personC
personD
personE
personF
personG
END
personA
personB
personC
personC
personD
personE
personE
personF
personG

Output:

What is the input file?
inut.dat
What is the Person Name?
personA
Maternal line:
personB
Paternal line:
personC
personE
personG
Children:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote