No matter how you arrange the three
methods inside the Demonstrate
class, your program refers to at
least one method before
that method is defined. In the following arrangement, for example, the
program refers to both previousMonth
and penultimateMonth
before they are defined. Fortunately, because the Java compiler is a
multiple-pass compiler, such forward references cause no problems.
public class Demonstrate { public static void main (String argv[]) { System.out.print("At the end of month 3, there are "); System.out.println(rabbits(3)); System.out.print("At the end of month 10, there are "); System.out.println(rabbits(10)); } public static int rabbits (int n) { if (n == 0 || n == 1) { return 1; } else {return previousMonth(n) + penultimateMonth(n);} } public static int previousMonth (int n) {return rabbits(n - 1);} public static int penultimateMonth (int n) {return rabbits(n - 2);} } --- Result --- At the end of month 3, there are 3 At the end of month 10, there are 89