Answer to Question #257511 in Java | JSP | JSF for Nikhil Sahoo

Question #257511

In Java, WAP to store name and age of 30 persons in two different arrays. Ask the age range from the use and then display name of those persons who are coming in that age group in descending order of their age. At the end display how many such persons found.


1
Expert's answer
2021-10-30T00:38:37-0400
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] names = new String[30];
        int[] ages = new int[30];
        Random random = new Random();
        for (int i = 0; i < names.length; i++) {
            names[i] = "Name" + i;
            ages[i] = random.nextInt(100) + 1;
            System.out.println(names[i]+" "+ages[i]);
        }
        System.out.println('\n');
        Scanner in = new Scanner(System.in);
        System.out.println("min:");
        int min = in.nextInt();
        System.out.println("max:");
        int max = in.nextInt();
        int size = 0;
        String[] namesRange = new String[30];
        int[] agesRange = new int[30];
        for (int i = 0; i < ages.length; i++) {
            if (ages[i] >= min && ages[i] <= max) {
                namesRange[size] = names[i];
                agesRange[size++] = ages[i];
            }
        }

        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (agesRange[i] < agesRange[j]) {
                    String name = namesRange[i];
                    namesRange[i] = namesRange[j];
                    namesRange[j] = name;
                    int age = agesRange[i];
                    agesRange[i] = agesRange[j];
                    agesRange[j] = age;
                }
            }
        }
        System.out.println("Number of persons: "+size);
        for (int i = 0; i < size; i++) {
            System.out.println(namesRange[i]+" "+agesRange[i]);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS