5.5 Review Exercises

Exercise 5.5

  1. Some methods return values while others do not.
    1. What is the difference between the forms of the definition of each of these kinds of methods?
    2. What is the difference between the calls to each of these kinds of methods?

  2. Explain the difference between an argument and a parameter.
  3. 
       
  4. What is the scope of a parameter of a method?
  5. 
       
  6. In the method outline shown here, state which variables can be used at each numbered line.
        int sample (int a, int b)
        {                                               // 1
    
          int c;
          ...                                           // 2
          for (int d = 0; .. )
          { 
            ...                                         // 3 
            int e;
            ...                                         // 4
    
          }
          ...                                           // 5
          {
            int f;
            ...                                         // 6
          }
          int g;
          ...                                           // 7
    
        }
  7. Suppose that two method definitions have these headers:
        A:   void foo (int m, float n)
        B:   void foo (float i, int j)
    For each of the following calls, state, with reasons, whether or not the call is valid and, if it is valid, which version of foo would be called.
    1. foo(8,4);
    2. foo(2L,3L);
    3. foo('A',5);
    4. foo(4,0.5);
    5. foo(7,6L);
    6. foo('x','y');
  8. Write a boolean-valued method isSquare with a single int parameter, n. The method should return the value true if and only if n is the square of some integer.
  9. Complete the definition of the method digit with header:
    int digit (int n, int position) 
    so that the method returns the value of the digit that is position places from the right in the decimal representation of n. As examples,

    digit (763,0) should return 3
    digit (8574,2) should return 5
    digit (78,4) should return 0

  10. Write a character-valued method convertToGrade that has an int parameter mark. The method should return the grade that corresponds to mark according to the following table.
    
    
    
    
    
  11. Write a method printTriangle that has a char parameter c and an int parameter n. The method should print a triangular pattern with the perimeter consisting of the character c and the interior (if there is one) consisting of blanks. As an example, the call printTriangle('*',5); should produce output of the form
    
    
    
    
    You may assume that the value of n is positive.