package am.utility.numeric;

import java.text.DecimalFormat;
import java.util.Arrays;

public class AvgMinMaxNumber {

	public String label;
	public double average;
	public double min;
	public int minCount;
	public double max;
	public double maxCount;
	public double median;

	
	public AvgMinMaxNumber( String label, int[] numbersList ) {
		this.label = label;
		computeStatistics(numbersList);
	}
	
	public AvgMinMaxNumber(int[] numbersList) {
		computeStatistics(numbersList);
	}	
	
	private void computeStatistics( int[] numbersList ) {
		// sort in ascending numerical order
		Arrays.sort(numbersList);

		long sum = 0;
		min = numbersList[0];
		minCount = 1;
		max = 0;
		maxCount = 0;
		for( int n : numbersList ) {
			sum += n;
			if( n < min ) {
				min = n;
				minCount = 1;
			} else if ( n == min ) {
				minCount++;
			}
			
			if( n > max ) {
				max = n;
				maxCount = 1;
			} else if ( n == max ) {
				maxCount++;
			}
		}

		average = (double) sum / (double) numbersList.length;

		if( numbersList.length % 2 == 0 ) {
			median = (double) ( numbersList[numbersList.length/2] + numbersList[(numbersList.length/2)-1] ) / 2d; 
		} else {
			median = (double) ( numbersList[ (numbersList.length-1)/2 ]);
		}
	}
	
	@Override
	public String toString() {
		DecimalFormat fmt = new DecimalFormat("###.###");
		
		String stats = new String("Average: " + fmt.format(average) + 
				", Min: " + fmt.format(min) + " (" + (int)minCount + " items)" +
				", Max: " + fmt.format(max) + " (" + (int)maxCount + " items)" +
				", Median: " + fmt.format(median));
		
		if( label == null )
			return stats;
		else
			return label + " -- " + stats; 
	}
}

@@ -224,30 +224,27 @@
 ax;%0A%09public 
-double
+int
  maxCount;%0A%09
@@ -264,16 +264,40 @@
  median;
+%0A%09public int totalCount;
 %0A%0A%09%0A%09pub
@@ -499,16 +499,1068 @@
 ;%0A%09%7D%09%0A%09%0A
+%09public AvgMinMaxNumber( String label, double%5B%5D numbersList ) %7B%0A%09%09this.label = label;%0A%09%09computeStatistics(numbersList);%0A%09%7D%0A%09%0A%09public AvgMinMaxNumber(double%5B%5D numbersList) %7B%0A%09%09computeStatistics(numbersList);%0A%09%7D%0A%09%0A%09// list of doubles.%0A%09private void computeStatistics( double%5B%5D numbersList ) %7B%0A%09%09// sort in ascending numerical order%0A%09%09Arrays.sort(numbersList);%0A%0A%09%09double sum = 0;%0A%09%09min = numbersList%5B0%5D;%0A%09%09minCount = 1;%0A%09%09max = 0;%0A%09%09maxCount = 0;%0A%09%09for( double n : numbersList ) %7B%0A%09%09%09sum += n;%0A%09%09%09if( n %3C min ) %7B%0A%09%09%09%09min = n;%0A%09%09%09%09minCount = 1;%0A%09%09%09%7D else if ( n == min ) %7B%0A%09%09%09%09minCount++;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if( n %3E max ) %7B%0A%09%09%09%09max = n;%0A%09%09%09%09maxCount = 1;%0A%09%09%09%7D else if ( n == max ) %7B%0A%09%09%09%09maxCount++;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09average = (double) sum / (double) numbersList.length;%0A%0A%09%09if( numbersList.length %25 2 == 0 ) %7B%0A%09%09%09median = (double) ( numbersList%5BnumbersList.length/2%5D + numbersList%5B(numbersList.length/2)-1%5D ) / 2d; %0A%09%09%7D else %7B%0A%09%09%09median = (double) ( numbersList%5B (numbersList.length-1)/2 %5D);%0A%09%09%7D%0A%09%09%0A%09%09totalCount = numbersList.length;%0A%09%7D%0A%09%0A%09// handle a list of integers%0A
 %09private
@@ -2289,16 +2289,54 @@
 %5D);%0A%09%09%7D%0A
+%09%09%0A%09%09totalCount = numbersList.length;%0A
 %09%7D%0A%09%0A%09@O
@@ -2454,17 +2454,40 @@
  String(
-%22
+ totalCount + %22 items.  
 Average:

Handle lists of doubles.