Here is a very simple Java program that generates age and gender statistics for a group of persons:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.HashMap; import java.util.Map; public class AgeGenderStatistics { public static void main(String[] args) { // Create a map to store the age and gender of each person Map<Integer, String> persons = new HashMap<>(); persons.put(34, "Male"); persons.put(20, "Female"); persons.put(40, "Male"); persons.put(25, "Female"); persons.put(30, "Male"); // Initialize the age and gender counters int maleCount = 0; int femaleCount = 0; int totalAge = 0; // Iterate through the map and update the counters for (Map.Entry<Integer, String> entry : persons.entrySet()) { int age = entry.getKey(); String gender = entry.getValue(); if (gender.equals("Male")) { maleCount++; } else if (gender.equals("Female")) { femaleCount++; } totalAge += age; } // Calculate the average age of the group double averageAge = (double) totalAge / persons.size(); // Print the age and gender statistics System.out.println("Number of males: " + maleCount); System.out.println("Number of females: " + femaleCount); System.out.println("Average age: " + averageAge); } } |
This program defines a map called persons, which stores the age and gender of each person as a key-value pair. The program then iterates through the map, updating counters for the number of males and females, and the total age of the group. Finally, it calculates the average age of the group and prints the age and gender statistics to the console.
You can modify this program by adding or removing persons from the persons map, or by changing the values in the map to reflect the ages and genders of the persons you want to include in your statistics.