```
public class MovingAverage {
int size, n, pos, sum;
int[] w;
public MovingAverage(int size) {
this.size = size;
w = new int[size];
sum=0;
}
public double next(int val) {
pos=pos % size;
sum-=w[pos];
w[pos++]=val;
sum+=val;
if(n<size)
n++;
return (double)sum/n;
}
}
```