Tutorial 1: Recursion and Test-Driven Practice

Unit 10 ยท Integration and capstone readiness

Objectives

A recursive method solves a problem by solving a smaller version of the same problem. Every valid input must move toward a base case. For factorial, the base case is 0! = 1.

static long factorial(int n) {
    if (n < 0) throw new IllegalArgumentException("n must be non-negative");
    if (n == 0) return 1;
    return n * factorial(n - 1);
}

Trace factorial(3) as calls down to factorial(0), then returns back upward. Tests should include normal, boundary, and invalid cases.

Practice

  1. Implement recursive sum from 1 through n.
  2. Write tests for zero, one, a typical value, and a negative input.
  3. Refactor duplicated test setup into a helper only when it improves clarity.

Self-check

  1. What happens if the base case is removed?
  2. Why should boundary cases be tested separately?
  3. What makes a test failure actionable?

Mastery task: Plan a capstone study tracker with a class model, file format, validation rules, five acceptance tests, and one deliberately failing test that you then fix.