/* -------------------------------------------------------- * * Aaron Morris * * CS567 * * Object-Oriented Event-Driven Simulation * * * * Generic Stats class, tracking statistics based on * * instances, not time. * * -------------------------------------------------------- */ //package datasend; import java.awt.*; public class Counter { //Members protected double last; // Last value updated with protected double sum; // Sum of all values updated with protected double sumsquared; // Sum of the squares of all values protected double max; // Max value updated with to date protected double min; // Min value updated with to date protected double n; // Number of values updated with protected String title; // Title of this report // Constructor -- Initialize Title and all members to 0 public Counter(String Title) { this.title = Title; n = 0; last = 0; sum = 0; sumsquared = 0; min = 0; max = 0; } // Method to return number of samples recorded public double getNumSamples() { return n; } // Method to update the counter with the value at Time public void update(double Value) { // If no samples yet, min and max are the first value if (n == 0) { min = Value; max = Value; } else if (Value < min) { min = Value; } // New min value else if (Value > max) { max = Value; } // New max value sum += Value; // Sum of all values updated with last = Value; // Set for next time update sumsquared += Value*Value; // Sum of all values squared n++; // Increment number of samples } // Print out results that have been recorded to output object public void doReport(Report r) { double variance,ex2,mean; r.write(title); if (n==0) { r.write(" n was 0, no data to report"); r.write(" "); } else { mean = sum/n; r.write(" sampled " + n + " values"); r.write(" values ranged from " + min + " to " + max); r.write(" mean is " + mean); ex2 = sumsquared / n; variance = ex2 - mean*mean; r.write(" variance is " + variance); r.write(" "); } } }