Menu
Question Index
...


Write a program to get the relations between family members.
Given the following family hierarchies.

Family Name Family Hierarchy
Rajeswara Rao's Family Rajeswara Rao-75M
+--Suresh-55M
|  +--Mahesh-35M
|  +--Lakshmi-32F
+--Ramesh-53M
|  +--Srinivas-30M
|     +--Kamala-8F
|     +--Raj-10M
Ramu's Family Ramu-60M
+--Rama-40M
|  +--Krishna-25M
|     +--Kumari-10F
|     +--Siva-12M
+--Ravi-43M
|  +--Anand-26M
|  +--Kishore-27M
|  +--Raghava-29M
|  +--Priya-28F
+--Rani-42F
|  +--Raghu-23M
|  +--Ramani-25F
|  +--Ramakrishna-24M
|     +--Raviteja-3M
|     +--Ramya-1F
Krishna's Family Krishna-75M
+--Sahithi-50F
|  +--Anandh-30M
|      +--Prem-3M
|  +--Rahul-27M
|      +--Kavya-1F
+--Soundarya-48F
|  +--Kavitha-26F
|      +--Puja-6F
|  +--Kumar-23M
+--Mahesh-42M
|  +--Roja-23F
|  +--Raja-20M
+--Sumanth-35M
|  +--Sravan-8M
Ganesh's Family Ganesh-50M
+--Arjun-25M
|  +--Ram-4M
|  +--Lakshman-4M
+--Kalyani-22F
|   +--Rupa-1F
|   +--Sreekanth-19M
|   +--Rajini-19F
|   +--Sulochana-19F
|   +--Shakti-19M
|   +--Lokesh-19M
|   +--Vishva-19M
|   +--Poorna-19F
+--Sirisha-20F
Charan's Family Charan-70M
+--Sukumar-48M
|  +--Priya-24F
|  +--Chandrika-22F
|  +--Kumar-19M
|  +--Mahesh-24M
+--Santhosh-45M
|  +--Ravi-22M
|  +--Raja-20M
+--Sakshi-40F
|  +--Vennela-20F
Ramana's Family Ramana-80M
+--Ravi-55M
|  +--Santhosh-30M
|     +--Sudha-1F
|  +--Pavan-20M
+--Kumar-53M
|  +--Sakshi-25F
|  +--Preethi-20F
+--Sowjanya-50F
|  +--Lavanya-35F
|     +--Suma-17F
|        +--Chanti-3F

class FindFamilyRelations

{    public static void main(String s[])
    {
        // Input as Rajeswara Rao's family as shown in the requirements
        Person rajeswaraRao = new Person("Rajeswara Rao", 'M', 75);
        Person suresh = new Person("Suresh", 'M', 55);
        Person ramesh = new Person("Ramesh", 'M', 53);
        rajeswaraRao.children.add(suresh);
        rajeswaraRao.children.add(ramesh);
        Person mahesh = new Person("Mahesh", 'M', 35);
        Person lakshmi = new Person("Lakshmi", 'F', 32);
        suresh.children.add(mahesh);
        suresh.children.add(lakshmi);
        Person srinivas = new Person("Srinivas", 'M', 30);
        ramesh.children.add(srinivas);
        Person kamala = new Person("Kamala", 'F', 8);
        Person raj = new Person("Raj", 'M', 10);
        srinivas.children.add(kamala);
        srinivas.children.add(raj);
        System.out.println("Lakshmi's parent is " + getParent(rajeswaraRao, "Lakshmi"));
        System.out.println("Youngest child in family is " + getYoungestChild(rajeswaraRao));
        System.out.println("Grandparent of Kamala is " + getGrandParent(rajeswaraRao, "Kamala"));
        System.out.println("The parent who has maximum number of children is " + getParentWithMaximumChildren(rajeswaraRao));
        System.out.println("The parent who has maximum sum of ages of the children is " + getParentWithMaximumSumOfChildrenAges(rajeswaraRao));

    }


private static Person getYoungestChild(Person root) {
//Write code here to get youngest child in the family
}

public static Person getParent(Person root, String name) {
//Write code here to find the parent of the person with the given name
}

private static Person getParentWithMaximumChildren(Person root) {
//Write code here to find the parent with maximum number of children
}

private static Person getParentWithMaximumSumOfChildrenAges(Person root) {
//Write code here to find the parent with sum of children ages is maximum
}

public static Person getGrandParent(Person root, String name) {
//Write code here to find the grand parent of the given person name
}

//Write additional methods here if required
}
class Person {

String name;
char gender;
int age;
List<Person> children = new ArrayList<Person>();

public Person(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}

@Override
public String toString() {
return name + "-" + age + gender;
}
}

Doubts

Problems

Topic: Java Collection Algorithms

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App