001package algs12;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac VisualAccumulator.java
005 *
006 *************************************************************************/
007
008
009public class XVisualAccumulator {
010        private double total;
011        private int N;
012
013        public XVisualAccumulator(int trials, double max) {
014                StdDraw.setXscale(0, trials);
015                StdDraw.setYscale(0, max);
016                StdDraw.setPenRadius(.005);
017        }
018
019        public void addDataValue(double val) {
020                N++;
021                total += val;
022                StdDraw.setPenColor(StdDraw.DARK_GRAY);
023                StdDraw.point(N, val);
024                StdDraw.setPenColor(StdDraw.RED);
025                StdDraw.point(N, mean());
026        }
027
028        public double mean() {
029                return total / N;
030        }
031
032        public String toString() {
033                return "Mean (" + N + " values): " + String.format("%8.5f", mean());
034        }
035
036        public static void main (String[] args) {
037                args = new String[] { "1000" };
038                int T = Integer.parseInt (args[0]);
039                XVisualAccumulator a = new XVisualAccumulator (T, 1.0);
040                for (int t = 0; t < T; t++)
041                        a.addDataValue (StdRandom.random ());
042                StdOut.println (a);
043        }
044}