8. วัตถุสิ่งของ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 8-23  คลาส TimeSeries เก็บข้อมูลอนุกรมเวลา
public class TimeSeries {
  private int      size = 0;
  private double[] data = new double[4];
   
  public void add(double v) {
    if (size == data.length) data = doubleCapacity(data);
    data[size++] = v;
  }
  public int size() {
    return size;
  }
  public double get(int k) {
    return data[k];
  }
  public double min() {
    if (size == 0) throw new IllegalStateException("size=0");
    double min = data[0];
    for (int i = 1; i < size; i++)
      if (data[i] < min) min = data[i];
    return min;
  }
  public double max() {
    if (size == 0) throw new IllegalStateException("size=0");
    double max = data[0];
    for (int i = 1; i < size; i++)
      if (data[i] > max) max = data[i];
    return max;
  }
  public TimeSeries getMovingAverage() {
    TimeSeries ma = new TimeSeries();
    ma.add((data[0] + data[1]) / 2);
    for (int i = 1; i <= size-2; i++) {
      ma.add((data[i-1] + data[i] + data[i+1]) / 3);
    }
    ma.add((data[size-2] + data[size-1]) / 2);
    return ma;
  }
  private static double[] doubleCapacity(double[] d) {
    double[] a = new double[2*d.length];
    for (int i=0; i<d.length; i++) a[i] = d[i];
    return a;
  }   
}
©2009 S.Prasitjutrakul