Problem 1: (30 points)
Consider the following interface:
          public interface DoubleGenerator {
             public double NextDouble();
          }
  1. Write a class EachHalf which implements DoubleGenerator. The first time NextDouble is called in an instance of this class it returns 1.0. The second time it returns 0.5, then 0.25, etc. Each time it returns half of the value returned the previous time.

  2. Write a class called CalcAverage whose constructor takes one parameter of type DoubleGenerator. It will have one public method called GetAverage which takes an integer parameter, count, and returns a double value. The value returned is the average of count values gotten from the NextDouble method of the DoubleGenerator.

Problem 2) (30 points)
Write down the output generated by the following code segment.

 
   private void init1(Point[] A) {
      for (int i=0;i < A.length;i++)
         A[i] = new Point(i,i*i);
   }
 
   private void init2(Point[] A) {
      for (int i=0;i < A.length;i++) {
         A[i].x = i;
         A[i].y = 3*i;
      }  
   }
 
   private void show(Point[] A, int n) {
      System.out.println("n = "+n+" ---");
      for (int i=0;i < A.length;i++)
         System.out.println("  "+i +": "+A[i].x+" "+A[i].y);
   }
   ...
 
   Point[] A = new Point[4];
   Point[] B = new Point[5];
   Point[] C = new Point[6];
   Point[] D = new Point[4];
   init1(A);
   show(A,1);
 
   init2(A);
   show(A,2);
 
   init1(B);
   B[2].x = 7;
   B[2].y = 11;
   show(B,3);
 
   init2(B);
   show(B,4);
 
   init1(C);
   C[2] = C[4];
   show(C,5);
 
   init2(C);
   show(C,6);
 
   A = C;
   show(A,7);
 
   init2(A);
   show(A,8);
   show(C,9);
 
   init2(D);
   show(D,10);

Next topic: ?