5.5 Solutions

    1. Those that return values specify the type of value returned; those that do not have void.
    2. For those that return values, the call is in a context appropriate for the type of value returned. For those that return no value, the call is alone in a statement.
  1. Argument: an expression whose value is passed to a method
    Parameter: a variable in a method header assigned the value of an argument
  2. The entire method
  3. 1: a b
    2: a b c
    3: a b c d
    4: a b c d e
    5: a b c
    6: a b c f
    7: a b c g
    1. Error, ambiguous case, equal amount of widening
    2. Error, at least one argument but be int
    3. Error, ambibuous case
    4. A, perfect match
    5. A, second argument can be widened to float
    6. Error, ambiguous case
  4.  
    boolean isSquare (int n)
    {
       return (int)sqrt(n)*(int)sqrt(n) == n;
    } 
  5.  
    int digit (int n, int position)
    {
       for (int i = 0; i < position; i++)
          n /= 10;
       return n % 10;
    } 
  6.  
    char convertToGrade (int mark)
    {
       if (mark >= 0 && mark < 50)
          return ’F’;
       else if (mark < 60)
          return ’D’;
       else if (mark < 70)
          return ’C’;
       else if (mark < 80)
          return ’B’;
       else if (mark <= 100)
          return ’A’;
       else
          return ’X’;
    } 
  7.  
    void printTriangle(char c, int n)
    {
      for (int i = 1; i <= n; i++)
      {
        for (int j = 1; j <= i; j++)
          if (j == 1 || j == i || i == n)
            print(c);
          else
            print(" ");
        println();
      }
    }