5.3 Solutions

    1. mystery
    2. a   b
    3. int
    4. int mystery(float a, float b)
    5.  
      int mystery (float a, float b)
      {
         int value;
         if (a < b)
            value = -1;
         else if (a > b)
            value = 1;
         else
            value = 0;
         return value;
      } 
    6.  
      int mystery (float a, float b)
      {
         if (a < b)
            return -1;
         else if (a > b)
            return 1;
         else
            return 0;
      } 
    1.  
      char first(char a, char b)
      {
         if(a < b)
            return a;
         else
            return b;
      } 
      Returns the smaller parameter.
    2.  
      float second(float a, float b)
      {
         float answer;
         if(a < b)
            answer = a - b;
         else
            answer = b - a;
         return answer;
      } 
      Returns the negative difference of the parameters.

  1. c) is invalid since println is a void method. It doesn't return anything to store in x.

    d) is valid, but pointless since the value of f(x) is lost.

  2.  
    float largest(float a, float b, float c)
    {
       if (a > b && a > c)
          return a;
       else if (b > c)
          return b;
       else
          return c;
    } 
  3.  
    int gcd (int x, int y)
    {
       int divisor = 1, small = abs(x);
       if (abs(y) < abs(x))
          small = abs(y);
       for (int i = 2; i <= small; i++)
          if (x % i == 0 && y % i == 0)
             divisor = i;
       return divisor;
    }