/* Provide Statistics on Annual Data // Constructor & Accessors public AnnualDataSet(double[] items) public double[] getData() public double getData(String month) // Other Methods public double average() public double minimum() public double maximum() public double median() public double standardDeviation() public String monthOfMaximum() public String monthOfMinimum() */ import java.util.*; public class AnnualDataSet { public static final int MONTHS_IN_YEAR = 12; public static final String[] MONTHS = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; //Instance Variable: 1 element for each month // data[0]: January, data[1]: February, ... data[11]: December private double[] data; //Constructor public AnnualDataSet(double[] items) { if (items.length != MONTHS_IN_YEAR ) throw new RuntimeException("12 Data Items Expected"); this.data = new double[MONTHS_IN_YEAR]; for (int index = 0; index < this.data.length; index++) this.data[index] = items[index]; } //Accessor - return all data items public double[] getData() { //return this.data; double[] info = new double[ this.data.length ]; for (int index = 0; index < info.length; index++) info[index] = this.data[index]; return info; } //Return the total of the data set private double total() { double sum = 0.0; /* for (int i = 0; i < data.length; i++) sum = sum + data[i]; */ for (double item : this.data) sum = sum + item; return sum; } //Return the average of the data set public double average() { return this.total() / this.data.length; } //Return the smallest item in the data set public double minimum() { double min = this.data[0]; for (double value : this.data) if ( value < min ) min = value; return min; } //Return the largest item in the data set public double maximum() { double max = this.data[0]; for (double value : this.data) if ( value > max ) max = value; return max; } //Return the median value of the data set public double median() { //Sort the data return (this.data[5] + this.data[6]) / 2.0; } //Return the standard deviation of the data set public double standardDeviation() { return Math.sqrt( this.variance() ); } //--------------------------------------------------------------------- //Accessor - return the data for a single month public double getData(String month) { for (int index = 0; index < this.data.length; index++) if ( month.equalsIgnoreCase( MONTHS[index] ) ) return this.data[ index ] ; throw new RuntimeException("Invalid Month Name " + month); } //Return the month with the highest data item in the set public String monthOfMaximum() { return MONTHS[ this.indexOfMax() ]; } //Return the month with the smallest data item in the set public String monthOfMinimum() { return MONTHS[ indexOfMin() ]; } //Helper: Find the index of the smallest data item private int indexOfMax() { int maxi = 0; for (int index = 0; index < this.data.length; index++) if ( this.data[index] > this.data[maxi] ) maxi = index; return maxi; } //Helper: Find the index of the smallest data item private int indexOfMin() { int mini = 0; for (int index = 0; index < this.data.length; index++) if ( this.data[index] < this.data[mini] ) mini = index; return mini; } //Helper: Return the variance of the data set private double variance() { double mean = this.average(); double total = 0.0; for (double item : this.data) total = total + (item - mean) * (item - mean); return total / this.data.length; } /* Alternative method implementations public double maximum() { return this.data[ this.indexOfMax() ]; } public double minimum() { return this.data[ this.indexOfMin() ]; } */ }