5.2 Solutions

  1. The fragment prints:
    i = 7 and j = 3

    The parameters get swapped but the original arguments are left untouched. Processing uses call by value when passing arguments, so a copy of the original arguments are swapped not the original arguments themselves.

  2. a) is invalid, first argument must be an int (0.5 is a float)
    c) is invalid, first argument must be an int (2.0 is a float)
    f) is invalid, first argument must be an int (3L is a long)
  3.  
    void square(int x, int y, int len)
    {
       rect(x, y, len, len);
    } 
    1.  
      void printRect (char c, int w, int h)
      {
         for (int i = 1; i <= h; i++)
         {
            for (int j = 1; j <= w; j++)
               print(c);
            println();
         }
      } 
    2.  
      void printRect (char c, int w, int h)
      {
         for (int i = 1; i <= h; i++)
         {
            for (int j = 1; j <= w; j++)
               if (i == 1 || i == h || j == 1 || j == w)
                  print(c);
               else
                  print(" ");
            println();
         }
      }