There are many possible solutions, here is a typical one:

  1. public static void subsets(int nint k) {
  2.     subsets(nk"");
  3. }
  4.  
  5. public static void subsets(int nint kString s) {
  6.     if (k == 0)
  7.         System.out.println(s);
  8.     else if (n > 0) {
  9.         subsets(n-1ks);
  10.         subsets(n-1k-1n+s);
  11.     }
  12. }

Approximate grading key for סעיף ג is as follows:

  • A +2 : correct base case condition (line 6)
  • B +1 : correct base case action (line 7)
  • C +3 : correct reduction case condition (line 8, n >= k is also OK, or just else with n > k condition for line 9 only)
  • D +6 : correct separation into two recursive calls (lines 9-10)
  • E +2 : correct use of String concatenation (n+s in line 10)

  • F 0..-3 : syntax errors
  • G 0..-3 : inelegant code
  • H -2 : concatenation of string accumulator in both/neither recursive calls
  • I 0..-3 : excessive parameters with no real use
  • J 0..-3 : wrong use of return/prints (i.e., misunderstanding the void)

Another nice solution:

  1. public static void subsets(int nint kString s) {
  2.     if (k == 0)
  3.         System.out.println(s);
  4.     else
  5.         for (int i = k;  i <= n;  ++i)
  6.             subsets(i-1k-1i+s);
  7. }

There are, of course, variants of either approach with extra parameters.

Another interesting alternative:

  1. public static void subsets(int nint kString s) {
  2.     if (k == 0)
  3.         System.out.println(s);
  4.     else if (n == k) {
  5.         for (int i = 1;  i <= n;  ++i)
  6.             s = s + i;
  7.         System.out.println(s);
  8.     }
  9.     else {
  10.         subsets(n-1k-1n+s);
  11.         subsets(n-1ks);
  12.     }
  13. }