Home Segments Top Top Previous Next

454: Mainline

Now, suppose that you rewrite the rabbits method in terms of two auxiliary methods:

public static int rabbits (int n) {
 if (n == 0 || n == 1) {
  return 1;
 }
 else {return previousMonth(n) + penultimateMonth(n);}  
} 

Realizing that previousMonth must return the number of rabbits at the end of the previous month, you see that you can define previousMonth as follows:

public static int previousMonth (int n) {return rabbits(n - 1);} 

Analogous reasoning leads you to the following definition for penultimateMonth:

public static int penultimateMonth (int n) {return rabbits(n - 2);}