next up previous
Next: What to turn in Up: Assignment 5: Recursion Previous: 99 Bottles

Recurse

1.
Create a new project called Recurse, based on the No Graphics stationery.

2.
Type in the method from Quiz 6 (it was called rarify there, but you should call it printBinary now that we know what it does).

3.
Write a few lines in main that test printBinary and run the program. Check that the output is correct.

4.
Make a copy of printBinary and rename it printBase. Generalize it by adding a second parameter called base and make it print numbers in any base instead of just base 2.

HINT: You do not have to understand the algorithm for converting from one base to another in order to do this assignment. One of the things I am suggesting is that generalizing a method can be a mechanical process you can perform without completely understanding the method.

5.
The Ackerman function is defined for non-negative integers as follows:


\begin{displaymath}A(m,n) = \left\{
\begin{array}{l@{\quad {\mathbf if} \quad }l...
..., n=0 \\
A(m-1, A(m, n-1)) & m>0, n>0 \\
\end{array} \right.
\end{displaymath} (1)

Write a method called ack that takes two ints as parameters and that computes and returns the Ackerman function. This will be the first non-void method you write; it will not print anything--it will only return an int.

6.
Test your implementation of Ackerman by invoking it from main and printing the return value. Warning: the return value gets very big very quickly. You should try it only for small values of m and n.

7.
Now that you have the hang of non-void methods, you can write a new version of printBase called convertBase that returns a String containing the result rather than printing the result.

HINT: You can compose String concatenation with the return statement in powerful ways. For example, if you have two pieces of the answer, you can assemble them and return them at the same time:

  public static String example () {
    String prefix = getPrefix ();
    String suffix = getSuffix ();
    return prefix + suffix;
  }


next up previous
Next: What to turn in Up: Assignment 5: Recursion Previous: 99 Bottles
Allen B. Downey
1999-03-01