Print Page Print Page

Ch5 – Recursive Methods

  1. Create a new class names Recursive and add the following method to the class. Also add the main method as follows.
public class Recursive {
  public static void countdown(int n){
    if(n ==0){
      System.out.println("Blastoff!");
    }
    else {
      System.out.println(n);
      countdown(n-1);
    }
  }
  public static void main(String[] args){
    Recursive.countdown(Integer.parseInt(args[0]));
  }
}
  1. Compile and run the method passing a number and note the result.
$ java Recursive 4
4
3
2
1
Blastoff!