Problem 1: (30 points)
public class EachHalf implements DoubleGenerator {

   private double nextval = 2.0;

   public double NextDouble() {
      nextval = nextval/2;
      return nextval;
   }
}

public class CalcAverage {

   private DoubleGenerator dg;

   public CalcAverage(DoubleGenerator dg) {
      this.dg = dg;
   }

   public double GetAverage(int n) {
      double sum;
      sum = 0;
      for (int i=0;i < n;i++)
         sum = sum + dg.NextDouble();
      return sum/n;
   }
}



Problem 3) (30 points)

n = 1 ---             n = 6 ---
  0: 0 0                0: 0 0
  1: 1 1                1: 1 3
  2: 2 4                2: 4 12
  3: 3 9                3: 3 9
n = 2 ---               4: 4 12
  0: 0 0                5: 5 15
  1: 1 3              n = 7 ---
  2: 2 6                0: 0 0
  3: 3 9                1: 1 3
n = 3 ---               2: 4 12
  0: 0 0                3: 3 9
  1: 1 1                4: 4 12
  2: 7 11               5: 5 15
  3: 3 9              n = 8 ---
  4: 4 16               0: 0 0
n = 4 ---               1: 1 3
  0: 0 0                2: 4 12
  1: 1 3                3: 3 9
  2: 2 6                4: 4 12
  3: 3 9                5: 5 15
  4: 4 12             n = 9 ---
n = 5 ---               0: 0 0
  0: 0 0                1: 1 3
  1: 1 1                2: 4 12
  2: 4 16               3: 3 9
  3: 3 9                4: 4 12
  4: 4 16               5: 5 15
  5: 5 25             java.lang.NullPointerException