package com.chapter_one;
import com.algs4.stdlib.StdDraw;
/***
* 一种能够累加数据的抽象数据类型
* @author Luodi
*
*/
public class VisualAccumulator {
private double total;
private int N = 0;
public VisualAccumulator(int trials, double max) {
/***
* 设置x轴的范围
*/
StdDraw.setXscale(0, trials);
/***
* 设置y轴的范围
*/
StdDraw.setYscale(0, max);
/***
* 设置画笔的大小
*/
StdDraw.setPenRadius(.005);
}
public void addDataValue(double val) {
N++;
this.total += val;
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.point(N, val);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.point(N, total / N);
}
/***
* 求平均值
* @return
*/
public double mean() {
return total / N;
}
public String toString() {
return "Mean(" + N + " values): " + mean();
}
public static void main(String[] args) {
int T = 1000;
VisualAccumulator a = new VisualAccumulator(T, 1.0);
for (int t = 0; t < T; t++) {
a.addDataValue(Math.random());
System.out.println(a);
}
}
}
1.2.4 一种能够累加数据的抽象数据类型
最新推荐文章于 2023-07-19 20:45:00 发布