|
From: Brendan M. <mc...@us...> - 2006-12-04 21:06:52
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv14849/src/edu/whitman/halfway/util Modified Files: SequenceStats.java Log Message: Index: SequenceStats.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/SequenceStats.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** SequenceStats.java 12 Jan 2004 20:06:43 -0000 1.2 --- SequenceStats.java 4 Dec 2006 21:06:47 -0000 1.3 *************** *** 8,57 **** /** A utility for keeping track of the min, max, and average of a * sequence of numbers, presented one at a time.*/ ! public final class SequenceStats{ ! int crntIndex; ! ! double min = Double.MAX_VALUE; ! int minIndex = -1; ! int minCount=0; //how many times this min occurred ! double max = -Double.MAX_VALUE; ! int maxIndex = -1; ! int maxCount=0; ! double sum = 0; ! public void show(double v){ ! sum += v; ! if(v > max){ ! max = v; ! maxIndex = crntIndex; ! maxCount = 1; ! }else if(v == max){ ! maxCount++; ! } ! if(v < min){ ! min = v; ! minIndex = crntIndex; ! minCount = 1; ! }else if(v == min){ ! minCount++; ! } ! crntIndex++; } - public String getStatString(){ - if(crntIndex == 0) - throw new IllegalArgumentException("Must put at least one number first."); - StringBuffer buf = new StringBuffer(); - buf.append("Mean of ").append(sum/(double)crntIndex).append(" from ").append(crntIndex).append(" values in range["); - buf.append(min).append(", ").append(max).append("]."); - buf.append(StringUtil.newline).append("Min occurred ").append(minCount).append(" times, "); - buf.append("Max occurred ").append(maxCount).append(" times."); - return buf.toString(); - } --- 8,80 ---- /** A utility for keeping track of the min, max, and average of a * sequence of numbers, presented one at a time.*/ ! public final class SequenceStats { ! int crntIndex; ! double min = Double.MAX_VALUE; ! int minIndex = -1; ! int minCount = 0; //how many times this min occurred ! double max = -Double.MAX_VALUE; ! int maxIndex = -1; ! int maxCount = 0; ! double sum = 0; ! public void show(double v) { ! sum += v; ! if (v > max) { ! max = v; ! maxIndex = crntIndex; ! maxCount = 1; ! } else if (v == max) { ! maxCount++; ! } ! if (v < min) { ! min = v; ! minIndex = crntIndex; ! minCount = 1; ! } else if (v == min) { ! minCount++; } + crntIndex++; + } + + public double getMax() { + return max; + } + + + public double getMin() { + return min; + } + + + public double getSum() { + return sum; + } + + public double getAverage() { + return sum/((double)crntIndex); + } + + + public String getStatString() { + if (crntIndex == 0) + throw new IllegalArgumentException("Must put at least one number first."); + + StringBuffer buf = new StringBuffer(); + buf.append("Mean of ").append(sum / (double) crntIndex).append(" from ") + .append(crntIndex).append(" values in range["); + buf.append(min).append(", ").append(max).append("]."); + buf.append(StringUtil.newline).append("Min occurred ").append(minCount) + .append(" times, "); + buf.append("Max occurred ").append(maxCount).append(" times."); + return buf.toString(); + + } |