class ArrayStats { /* * Find the maximum of an integer array */ static int max(int[] a) { assert a != null : "Null array passed to max"; assert a.length > 0 : "Empty array passed to max"; int biggest = a[0]; for (int n: a) if (n > biggest) biggest = n; return biggest; } /* * Find the sum of an integer array */ static int sum(int[] a) { assert a != null : "Null array passed to sum"; assert a.length > 0 : "Empty array passed to sum"; int sum = 0; for (int n: a) sum += n; return sum; } /* * Find the average of an integer array */ static double average(int[] a) { assert a != null : "Null array passed to average"; assert a.length > 0 : "Empty array passed to average"; return (double)sum(a) / (double)a.length; } /* * Driver to test the above methods */ public static void main(String[] args) { int[] numbers = {29, 47, -8, 51, 13}; System.out.print("List: "); for (int n: numbers) System.out.print(n + " "); System.out.println(); System.out.println("Max = " + max(numbers)); System.out.println("Sum = " + sum(numbers)); System.out.println("Average = " + average(numbers)); } } // end of ArrayStats class