public int factorial(int n) {
    if (n == 1) {
      return 1;  // base case
    }
    return n * factorial(n - 1);  // recursive case
  }

In this example, the factorial method calculates the factorial of a given number n. The method uses a base case of n == 1, which means that if the input is 1, the method returns 1 without making any further recursive calls. This is important because it provides a stopping point for the recursion.

In the recursive case, the method returns n * factorial(n - 1). This means that the method will call itself with n - 1 as the input. This will continue until the base case is reached, at which point the recursive calls will start returning and the final result will be calculated and returned.

Here is an example of how this method can be used:

int result = factorial(5);  // calls factorial(5), which returns 5 * factorial(4)
                            // calls factorial(4), which returns 4 * factorial(3)
                            // calls factorial(3), which returns 3 * factorial(2)
                            // calls factorial(2), which returns 2 * factorial(1)
                            // calls factorial(1), which returns 1
                            // the recursive calls start returning:
                            // factorial(2) returns 2 * 1 = 2
                            // factorial(3) returns 3 * 2 = 6
                            // factorial(4) returns 4 * 6 = 24
                            // factorial(5) returns 5 * 24 = 120
                            // the final result is 120

Recursion can be a powerful tool for solving problems, but it's important to make sure that the base case is well-defined and that the recursion will eventually come to an end. Otherwise, the method will continue to call itself indefinitely, which can lead to stack overflow and other errors.

Here is an example of recursive code in action through printing numbers

public class RecursiveExample {

    public static void main(String[] args) {
  
      // Print the numbers from 1 to 10
      printNumbers(1, 10);
    }
  
    // Recursive function to print the numbers from start to end
    public static void printNumbers(int start, int end) {
  
      // If the start number is greater than the end number, return
      if (start > end) {
        return;
      }
  
      // Print the start number
      System.out.println(start);
  
      // Call the function again with the start number incremented by 1
      printNumbers(start + 1, end);
    }
  }

  RecursiveExample.main(null);
1
2
3
4
5
6
7
8
9
10

World cup recursive hack(extra credit)

public class WorldCup {

    // Recursive function to simulate a team advancing through the tournament
    public static void advanceTeam(String teamName, int roundsWon) {
  
      // If the team has won the required number of rounds, they have won the tournament
      if (roundsWon == 7) {
        System.out.println(teamName + " has won the World Cup!");
        return;
      }
  
      // Simulate the team winning a match in the current round
      System.out.println(teamName + " has won a match in round " + (roundsWon + 1));
  
      // Advance the team to the next round by calling the function again
      advanceTeam(teamName, roundsWon + 1);
    }
  
    public static void main(String[] args) {
  
      // Simulate a team winning the World Cup
      advanceTeam("Brazil", 0);
    }
  }

  
  WorldCup.main(null);
Brazil has won a match in round 1
Brazil has won a match in round 2
Brazil has won a match in round 3
Brazil has won a match in round 4
Brazil has won a match in round 5
Brazil has won a match in round 6
Brazil has won a match in round 7
Brazil has won the World Cup!

In this program, the advanceTeam() function is called recursively to simulate a team advancing through the tournament by winning matches. Each time the function is called, the number of rounds won is incremented and checked to see if the team has won the required number of rounds to win the tournament. If so, a message is printed and the function returns. Otherwise, the function is called again to simulate the team advancing to the next round. This continues until the team has won the tournament, at which point the function returns and the program ends.