Write a program that will ask a user to enter names and ages of people, stopping when an age of 0 is entered (and not processing that person). The program should collect this information, and then output the average age, the name of the oldest person, and the name of the youngest person. Assume no two people have the same age

Respuesta :

Answer:

package b4;

import java.util.ArrayList;

import java.util.Scanner;

public class ToThePowerOf {

public static void main(String[] args) {

 //Create an object of the scanner class to allow for user inputs

 Scanner input = new Scanner(System.in);

 // Since the number of persons are not known before hand,

 // using arraylists to hold the names and ages is a better idea

 // Arraylists are flexible

 ArrayList<String> names = new ArrayList<>();

 ArrayList<Integer> ages = new ArrayList<>();

 // variable name to temporarily hold the current name that is being entered

 String name = "";

 // variable age to temporarily hold the current age that is being entered

 int age = 0;

 // creating an infinite loop

 // loop will only end when the user enters a value of 0 for age

 while (true) {

  System.out.println("Please enter name");

  name = input.next();

  System.out.println("Please enter age");

  age = input.nextInt();

  // only add the name and age to the arraylists if the age entered is greater

  // than zero(0)

  if (age > 0) {

   names.add(name);

   ages.add(age);

  } else {

   // break out of the loop if the user enters a value of zero(0) for age

   break;

  }

 }

 // You might want to print out the names and ages

 System.out.println(names.toString());

 System.out.println(ages.toString());

 // Get and display the average age, oldest person and youngest person

 getAverageAge(ages);

 getNameOfOldest(ages, names);

 getNameOfYoungest(ages, names);

}

// Method to get the name of the oldest person

public static void getNameOfOldest(ArrayList<Integer> ages, ArrayList<String> names) {

 // Store the first value in the ages arraylist temporarily

 // Also store the corresponding index of the first value which is zero

 // Don't forget, arraylists are indexed from zero.

 int temp = ages.get(0);

 int index = 0;

 // Now start loop at 1,

 // since the first element (at zero(0)) has been stored temporarily in variable

 // temp above.

 // Loop through the arraylist and get the index of the smallest element

 // in the ages arraylist.

 for (int i = 1; i < ages.size(); i++) {

  if (ages.get(i) > temp) {

   temp = ages.get(i);

   index = i;

  }

 }

 // Using the index of the biggest element, get the corresponding name from the

 // names arraylist

 System.out.println("Name of oldest is " + names.get(index));

}

// Method to get the name of the youngest person

public static void getNameOfYoungest(ArrayList<Integer> ages, ArrayList<String> names) {

 // Store the first value in the ages arraylist temporarily

 // Also store the corresponding index of the first value which is zero

 // Don't forget, arraylists are indexed from zero.

 int temp = ages.get(0);

 int index = 0;

 // Now start loop at 1,

 // since the first element (at zero(0)) has been stored temporarily in variable

 // temp above.

 // Loop through the arraylist and get the index of the smallest element

 // in the ages arraylist.

 for (int i = 1; i < ages.size(); i++) {

  if (ages.get(i) < temp) {

   temp = ages.get(i);

   index = i;

  }

 }

 // Using the index of the smallest element, get the corresponding name from the

 // names arraylist

 System.out.println("Name of youngest is " + names.get(index));

}

public static void getAverageAge(ArrayList<Integer> ages) {

 // Initialize sum to zero.

 int sum = 0;

 // Using enhanced for loop, find the sum of elements in the ages arraylist

 for (int age : ages) {

  sum += age;

 }

 // Using the result of sum, divide by the length or size of the arraylist

 // to get the average

 double average = (double) sum / ages.size();

 System.out.println("The average age is " + average);

}

}

Explanation:

Explanation has been given in the code as comments. You might want to get the source code attached to this answer for more readability. File is saved as "AgeStatistics.java"

Hope this helps!

Ver imagen stigawithfun