/*
Declare, create, initialize, and use arrays.
Coded live. Comments added later.
Problem: Input age of peoeple, find max and average, and print.
Aditya Mathur, Feb 4, 2018
 */
import java.util.Scanner;
public class ArrayInClassExamples {
    public static void main(String[] args){
        int [] age; // Declare an array named "age"
        final int maxSize=10;
        age=new int[maxSize]; // Create array age

        // Get a string of ages from console.
        String data;
        Scanner s=new Scanner(System.in);
        System.out.print("Enter ages: ");
        data=s.nextLine(); // Read one line from console. This contains ages separated by comma.
       // System.out.println(data);

        // Get input data from console; split it, and assign to age.
        String []ageData=new String[maxSize];
        ageData=data.split(",");  // Split data in the input string using comma as a separator.
        for (int i=0; i<ageData.length; i++){
            age[i]=Integer.parseInt(ageData[i]); // Note: Input is read as a string
            System.out.println(age[i]);
        }
        // Find average age. Step 1: Add all ages.
        int sum=0;
        for (int i=0; i<ageData.length; i++){
            sum=sum+age[i];
        }

        // Compute  and print average.
        double avg=sum/ageData.length;
        System.out.println("Average: "+avg);

        // Find maximum age.
        int max;
        max=age[0]; // Suppose that the first element of age is the largest of all.

        for (int i=1; i<ageData.length; i++) {
            if (max < age[i]) { // Compare max with the next element of age.
                max = age[i]; // max is not the largest, so reset it to the next element.
            }
        }// End of for
        System.out.println("Max age: "+max);


    }// End of main
}// End of class.
