CSCI 2310
Fall 2009
Exercises for Quiz 3

  1. The mean of three numbers is their sum divided by 3. Write a static method mean(x,y,z) that returns the mean of x, y and z, using the following heading.

      public static double mean(double x, double y, double z)
    

    Answer

  2. The geometric mean of two numbers x and y is the square root of the product of x and y. Write a static Java method called geometricMean that returns the geometric mean of its two parameters. Note. You can use function Math.sqrt to compute square roots. Math.sqrt(x) is the square root of x.

          public static double geometricMean(double x, double y)  
    
    

    Answer

  3. Using method geometricMean from the previous exercise, write a program fragment that sets variable z to the geometric mean of variable r and twice variable s. Assume that r and s have type double.

    Answer

  4. Static method f is defined as follows.

         public static int f(int x)
         {
           int k = 1;
           while(k <= x) {
             k = k + k;
           }
           return k;
         }
    
    What are f(3) and f(f(3))?
       f(3)    = _________
    
    
       f(f(3)) = _________
    
    

    Answer