Define class Palace – Visit Status

N persons visit a palace. The namegender(M-Male or F-Female) and age of the N persons are passed as the input. The program must find the number of males and number of females visited the palace in each age. For each age (ascending order), the program must print the age, the number of males, the number of females and the names of the persons. In each age group, the names of the persons must be printed in the order of their occurrence.

Your task is to define the class Palace so that the program runs successfully.

Example Input/Output 1:
Input:
7
Hector M 25
Arun M 25
Lavanya F 25
Oliver M 22
Catherine F 24
Jhanvi F 23
Kavin M 23

Output:
22 1 0
Oliver
23 1 1
Jhanvi Kavin
24 0 1
Catherine
25 2 1
Hector Arun Lavanya

Explanation:
Here N = 7.
Age 22: 1 male 0 females (Oliver).
Age 23: 1 male 1 female (JhanviKavin).
Age 24: 0 males 1 female (Catherine).
Age 25: 2 males 1 female (HectorArunLavanya).

Example Input/Output 2:
Input:
13
Arun M 45
Babloo M 37
Rachel F 45
John M 45
Deepa F 37
Gavin M 25
Nancy F 45
Pavithra F 45
Mambo M 45
Anitha F 50
Helen F 37
Pravin M 45
Bhuvana F 45

Output:
25 1 0
Gavin
37 1 2
Babloo Deepa Helen
45 4 4
Arun Rachel John Nancy Pavithra Mambo Pravin Bhuvana
50 0 1
Anitha

import java.util.*;
class Palace{
    String[] name=new String[100];
    char[] gen=new char[100];
    int[] age=new int[100];
    int i=0;

    public void visit(String cp,char g,int a){
        name[i]=cp;
        gen[i]=g;
        age[i]=a;
        i++;
    }

    public void printVisitStatus(){
        int[] age2=new int[i];
        for(int a=0;a<i;a++){
            age2[a]=age[a];
        }
        for(int a=0;a<i;a++){
            for(int b=a+1;b<i;b++){
                if(age2[a]>age2[b]){
                    int t=age2[a];
                    age2[a]=age2[b];
                    age2[b]=t;
                }
            }
        }
        int[] age3=new int[i];
        int x=0;
        for(int a=0;a<i-1;a++){
            if(age2[a]!=age2[a+1]){
                age3[x++]=age2[a];
            }
        }
        age3[x++]=age2[i-1];
        for(int j=0;j<x;j++){
            int mc=0,fc=0;
            for(int p=0;p<i;p++){
                if(age[p]==age3[j] && gen[p]=='M'){
                    mc+=1;
                }
                if(age[p]==age3[j] && gen[p]=='F'){
                    fc+=1;
                }
            }
            System.out.println(age3[j]+" "+mc+" "+fc);
            for(int p=0;p<i;p++){
                if(age[p]==age3[j]){
                    System.out.print(name[p]+" ");
                }
            }
            System.out.println();
        }
    }
}
public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.nextLine().trim());
        Palace palace = new Palace();
        for (int ctr = 1; ctr <= N; ctr++) {
            String currPerson[] = sc.nextLine().trim().split("\s+");
            palace.visit(currPerson[0], currPerson[1].charAt(0), Integer.parseInt(currPerson[2]));
        }
        palace.printVisitStatus();
    }
}
Previous Article

Merge Three Arrays - Ascending Order

Next Article

Rearrange Mixed Matrix

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *