1	//    File:  ArrayExample.java  -  show use of 1-D arrays of int's

     2	import java.io.PrintWriter;

     3	public class ArrayExample {

     4	    // The actual "main routine" -- the class constructor
     5	    ArrayExample() {
     6	        int[] nInts;
     7	        nInts = loadArray();
     8	        displayArray("Initially", nInts);
     9	        System.out.println("Maximum value:  " + max(nInts));
    10	        System.out.println("Average value:  " + average(nInts));
    11	        System.out.printf("Average value: %7.3f\n\n", average(nInts));
    12	        
    13	    }

    14	    // create a array loaded with random int's in range 1..NUM
    15	    int[] loadArray() {
    16	        int[] nArray = 
			     new int[(int) ((MAX_SIZE - MIN_SIZE) * Math.random() + MIN_SIZE)];
    17	        for (int i = 0; i < nArray.length; i++)
    18	            nArray[i] = (int) (NUM * Math.random() + 1);
    19	        return nArray;
    20	    }

    21	    // display the array preceded by a title
    22	    void displayArray(String sTitle, int[] nArray) {
    23	        System.out.print(sTitle + "(" + nArray.length + "): ");
    24	        for (int i = 0; i < nArray.length; i++)
    25	            System.out.print(" " + nArray[i]);
    26	        System.out.println();
    27	    }

    28	    // find the maximum of a linear array of int's
    29	    int max(int[] nArray) {
    30	        int nMax = -1;
    31	        if (nArray != null && nArray.length >= 1) {
    32	            nMax = nArray[0];
    33	            for (int i = 1; i < nArray.length; i++)
    34	                if (nArray[i] > nMax)
    35	                    nMax = nArray[i];
    36	        }
    37	        return nMax;
    38	    }

    39	    // find the average of a linear array of int's
    40	    double average(int[] nArray) {
    41	        int nSum = 0;
    42	        for (int i = 0; i < nArray.length; i++)
    43	            nSum += nArray[i];
    44	        return ((double) nSum) / nArray.length;
    45	    }

    46	    // the main routine, the mainline
    47	    public static void main(String[] args) {
    48	        System.out.println("ArrayExample...\n");
    49	        for (int i = 0; i < 5; i++)
    50	            new ArrayExample();
    51	    }

    52	    private static final int MIN_SIZE = 10;        // array of 10
    53	    private static final int MAX_SIZE = 15;        // to 15 entries
    54	    private static final int NUM      = 20;        // max entry size
    55	}

    56	/* ===== output from the program =====

    57	ArrayExample...

    58	Initially(12):  12 18 1 18 15 12 13 1 7 5 7 5
    59	Maximum value:  18
    60	Average value:  9.5
    61	Average value:   9.500

    62	Initially(11):  11 7 8 5 4 4 12 19 13 9 11
    63	Maximum value:  19
    64	Average value:  9.363636363636363
    65	Average value:   9.364

    66	Initially(14):  11 2 1 5 13 16 11 17 7 1 13 6 17 19
    67	Maximum value:  19
    68	Average value:  9.928571428571429
    69	Average value:   9.929

    70	Initially(13):  19 2 7 6 12 15 6 5 9 8 13 4 14
    71	Maximum value:  19
    72	Average value:  9.23076923076923
    73	Average value:   9.231

    74	Initially(11):  14 13 19 13 20 7 14 3 3 8 15
    75	Maximum value:  20
    76	Average value:  11.727272727272727
    77	Average value:  11.727

    78	===== end of output ===== */