001package algs12; 002 003import java.util.Arrays; 004import stdlib.*; 005 006public class XArrayStats { 007 private double theMin; 008 private double theMax; 009 private double theMean; 010 public XArrayStats (double[] array) { 011 double min = Double.POSITIVE_INFINITY; 012 double max = Double.NEGATIVE_INFINITY; 013 double sum = 0; 014 for (double d : array) { 015 if (d < min) min = d; 016 if (d > max) max = d; 017 sum += d; 018 } 019 theMin = min; 020 theMax = max; 021 theMean = sum/array.length; 022 } 023 public double min () { return theMin; } 024 public double max () { return theMax; } 025 public double mean () { return theMean; } 026 027 028 public static void main (String[] args) { 029 double[] a = { 2, 6, -5, 9, 8, -1, 0 }; 030 XArrayStats stats = new XArrayStats (a); 031 StdOut.println (Arrays.toString (a)); 032 StdOut.format ("min=%f, max=%f, mean=%f\n", stats.min(), stats.max(), stats.mean() ); 033 } 034}