/* -------------------------------------------------------- * * Aaron Morris * * CS567 * * Object-Oriented Event-Driven Simulation * * * * Extends Generic Stats class, tracking statistics based * * on time, not instances. * * -------------------------------------------------------- */ //package datasend; import java.awt.*; public class Averager { protected double lastTime; // Last time Averager updated protected double last; // Last value Averager updated with protected double sum; // Running sum of all update values protected double sumsquared; // Running sum of all values squared 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 statistics report // Constructor -- Initialize Title, and all members to 0 public Averager(String Title) { this.title = Title; n = 0; last = 0; sum = 0; sumsquared = 0; min = 0; max = 0; lastTime = 0; } // Return number of samples that Averager has been updated with public double getNumSamples() { return n; } // Update method -- update the Averager for the increment of time // between lastTime and the Current Time with the Value sent in public void update(int Time, double Value) { // If this is the first value, min and max are the same 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 // Update for time interval from last update to this Time sum += Value*(Time-lastTime); last = Time; sumsquared += Value*Value * (Time - lastTime); lastTime = Time; n++; } // Print report of all the statistics gathered to the output object public void doReport(Report r) { double mean, ex2, variance; r.write(title); if (last == 0) { r.write(" Time was 0, no stats to report"); r.write(" "); } else { mean = sum / last; r.write(" observed for: " + this.last + " units of time"); r.write(" observations ranged from " +min+ " to " + max); r.write(" mean is " + mean); ex2 = sumsquared / last; variance = ex2 - mean*mean; r.write(" variance is " + variance); r.write(" "); } } }