4.16 Recursion
Learn recursion through a mission countdown, call-stack tracing, factorial fuel calculations, and launch-code checksums.
1. LxD Cycle Process
Empathize: Students can see that a method calls itself but often cannot explain when calls stop or how values return through the call stack. A recursive call that does not get closer to its base case can also crash the program.
Define:
- POV: CSA students need to identify the base case, recursive call, and progress toward the base case because all three parts are required for correct recursion.
- Learning Goal: Students will write, trace, and explain recursive methods with a base case and a recursive call that makes progress.
Ideate:
- HMW Question: How might we make a rocket mission show the stack building and unwinding?
- HMW Question: How might we connect recursive math to a meaningful mission task?
- Activity: Run a countdown, calculate mission fuel, and check a launch-code checksum recursively.
Prototype & Test: A peer ran the mission examples and found that the original homework was already solved. I changed the runners to complete Java classes, added visible output to every example, and left the power method with a compilable TODO result for students to repair.
2. Lesson Plan
Learning Objective: Identify the base case and recursive case, trace a recursive call stack, and write recursive methods that move toward a base case.
Success Criteria: You can identify where recursion stops, identify the recursive call and its progress, trace power(2, 3) by hand, compare print-before and print-after output, and repair a broken recursive method.
Tech Talk (5 minutes)
Recursion means a method solves a problem by calling itself on a smaller version of the problem. The base case is the condition where the method stops making calls. The recursive case is the part that calls the method again. The argument must move toward the base case, such as changing number to number - 1. Recursion is sometimes used instead of a loop when a problem naturally breaks into smaller versions of itself, such as walking through nested data.
Our running scenario is a launch mission: a countdown starts the launch, factorial calculates fuel units, and a digit sum checks the launch code.
Call Stack Trace: factorial(3)
A call stack is the stack of method calls that are currently waiting for results. Each call creates a frame containing that call’s parameter values and paused work.
Code Runner Challenge
Change the starting number and predict the mission output
View IPYNB Source
1. factorial(3) starts and waits for 3 * factorial(2)
2. factorial(2) starts and waits for 2 * factorial(1)
3. factorial(1) reaches the base case and returns 1
4. factorial(2) resumes: 2 * 1 = 2, then returns 2
5. factorial(3) resumes: 3 * 2 = 6, then returns 6
Unwinding is the process of returning from the base case back through the paused frames in reverse order: 1, then 2, then 6. The stack grows while calls move toward the base case and unwinds after the base case returns.
From the College Board
AP CSA Unit 4, Topic 4.16 focuses on recursion. Recursive solutions use a method that calls itself and must include a base case. The recursive call should solve a smaller part of the problem so the base case is eventually reached.
3. Reference Guide
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Recursion | A method solving a problem by calling itself on a smaller problem. | factorial(number - 1) |
| Base case | Condition that stops further calls. | if (number <= 1) |
| Recursive case | Part that calls the method again. | return number * factorial(number - 1); |
| Call stack | Stack of paused method calls waiting for a result. | factorial(3) waits for factorial(2) |
| Frame | One call’s parameters and paused work on the call stack. | The number value in factorial(2) |
| Unwinding | Returning results from the base case back upward. | 1, then 2, then 6 |
| StackOverflowError | Runtime error caused by recursion that does not stop. | A call that keeps the same argument |
Recursion Checklist
- What is the base case?
- What does it return or print?
- What is the recursive call?
- Does its argument move toward the base case?
- What happens while the calls unwind?
Recursion Compared with Loops
- A loop repeats through a condition and update.
- Recursion repeats through method calls and smaller arguments.
- Both need progress and a stopping condition.
4. Code Examples
A. Countdown: Print Before the Recursive Call
This runner prints each countdown stage before making the smaller recursive call, so the output moves toward liftoff.
Code Runner Challenge
Predict the completion order, then run the mission
View IPYNB Source
// CODE_RUNNER: Change the starting number and predict the mission output
public class MissionCountdown {
public static void countdown(int number) {
if (number == 0) {
System.out.println("Liftoff!");
} else {
System.out.println("T-minus " + number);
countdown(number - 1);
}
}
public static void main(String[] args) {
countdown(3);
}
}
MissionCountdown.main(null);
B. Unwinding: Print After the Recursive Call
This runner waits for the smaller call to finish before printing, so the completion messages appear in reverse order.
Code Runner Challenge
Predict factorial(4), then run the fuel calculation
View IPYNB Source
// CODE_RUNNER: Predict the completion order, then run the mission
public class MissionUnwinding {
public static void completeStage(int number) {
if (number == 0) {
System.out.println("Launch sequence ready");
} else {
completeStage(number - 1);
System.out.println("Stage " + number + " complete");
}
}
public static void main(String[] args) {
completeStage(3);
}
}
MissionUnwinding.main(null);
When a message is printed before the recursive call, it appears while the stack is growing; when it is printed after the call, it appears while the stack is unwinding, so the order reverses.
C. Factorial Fuel
This runner recursively calculates factorial fuel units by multiplying the current mission number by the result of a smaller problem.
Code Runner Challenge
Trace the launch code, then change it and predict the checksum
View IPYNB Source
// CODE_RUNNER: Predict factorial(4), then run the fuel calculation
public class MissionFuel {
public static int factorial(int number) {
if (number <= 1) {
return 1;
}
return number * factorial(number - 1);
}
public static void main(String[] args) {
System.out.println("Fuel units for mission 4: " + factorial(4));
}
}
MissionFuel.main(null);
D. Digit-Sum Checksum
This runner recursively adds the digits of the launch code to produce a checksum.
Code Runner Challenge
Run it to see why recursive progress toward the base case matters
View IPYNB Source
// CODE_RUNNER: Trace the launch code, then change it and predict the checksum
public class LaunchCodeChecksum {
public static int sumDigits(int number) {
if (number < 10) {
return number;
}
return (number % 10) + sumDigits(number / 10);
}
public static void main(String[] args) {
int launchCode = 2026;
System.out.println("Checksum for " + launchCode + ": " + sumDigits(launchCode));
}
}
LaunchCodeChecksum.main(null);
For sumDigits(2026), the trace is 6 + sumDigits(202) → 6 + 2 + sumDigits(20) → 6 + 2 + 0 + sumDigits(2) → 6 + 2 + 0 + 2 = 10. number % 10 gives the last digit, while integer division number / 10 removes that digit. Factorial is the mission’s fuel because its multiplication grows a resource total; digit sum is the launch-code checksum because it reduces the code to a quick value for checking.
E. Broken Runner
This runner keeps passing the same argument, so it never reaches the base case; the caller catches the resulting StackOverflowError without printing inside the recursive method.
Code Runner Challenge
Predict the count-up order, then run it
View IPYNB Source
// CODE_RUNNER: Run it to see why recursive progress toward the base case matters
public class BrokenMissionCountdown {
public static void countdown(int number) {
if (number == 0) {
return;
}
countdown(number);
}
public static void main(String[] args) {
try {
countdown(3);
} catch (StackOverflowError error) {
System.out.println("Stack overflow: the recursive call never moved toward zero.");
}
}
}
BrokenMissionCountdown.main(null);
5. Hacks & Practice Tasks
Prepare Your Submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add a raw frontmatter cell.
- Add a markdown cell identifying the base case, recursive case, argument progress, and a written call-stack trace of
power(2, 3). - Add runnable Java cells for the Popcorn Hack and Homework Hack.
- Run each cell and leave the output visible.
- End every Java cell with
ClassName.main(null);.
Popcorn Hack (In-Class)
Write a recursive count-up method that starts at 1 and prints through 5. Identify its base case and explain how the argument moves toward it before you run the cell.
Code Runner Challenge
Complete both TODOs, then run the power tests
View IPYNB Source
// CODE_RUNNER: Predict the count-up order, then run it
public class MissionCountUp {
public static void countUp(int number) {
if (number > 5) {
System.out.println("Count-up complete");
} else {
System.out.println(number);
countUp(number + 1);
}
}
public static void main(String[] args) {
countUp(1);
}
}
MissionCountUp.main(null);
Homework Hack
Complete both TODOs in the recursive power method. The base case must handle an exponent of zero, and the recursive case must use a smaller power problem. Then write a markdown trace of power(2, 3), naming each call and each returned value.
Stretch task: What happens if the exponent is negative? Explain why the current base case does not stop that input and what a robust method would need to do.
// CODE_RUNNER: Complete both TODOs, then run the power tests
public class MissionPowerHomework {
public static int power(int base, int exponent) {
if (exponent == 0) {
// TODO: What should power return when the exponent is 0?
return 0;
}
// TODO: Multiply base by the answer to a smaller power problem.
return 0;
}
public static void main(String[] args) {
System.out.println("2^3 = " + power(2, 3));
System.out.println("3^2 = " + power(3, 2));
}
}
MissionPowerHomework.main(null);
6. Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Homework base case | 0.2 | Student writes a correct stopping case for exponent 0. |
| Homework recursive case | 0.2 | Student writes a recursive call that solves a smaller power problem. |
| Call-stack trace | 0.2 | Markdown traces power(2, 3) calls and returned values in the correct order. |
| Broken runner repair | 0.2 | Student explains the missing progress and repairs the recursive call. |
| Popcorn and code quality | 0.2 | Count-up runner has a base case, makes progress, prints output, and ends with its class invocation. |
| Total | 1.0 |
Quick Validation Checklist
- Every runner is a complete Java class with
main. - Every runner declares its own variables and prints visible output.
- Print-before and print-after runners show different output orders.
- The factorial fuel and digit-sum checksum runners match their descriptions.
- The broken runner catches
StackOverflowErrorwithout printing from the recursive method. - Popcorn uses a recursive count-up that is not duplicated in Section 4.
- Homework starts with both base-case and recursive-case TODOs.
- The markdown submission includes a correct
power(2, 3)call-stack trace. - The broken recursive call is repaired and the negative-exponent stretch is explained.
7. Lesson Revisions & Feedback Evidence
Feedback Received: A peer noted that the first draft had solved homework, an incomplete runner sequence, and unrelated math examples that did not form a coherent learning story.
Revision Made: The lesson now follows a launch-mission scenario, gives every example a complete Java runner, connects countdown, fuel, and checksum tasks, and leaves the power recursive case as a student TODO.
References
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025].
- CSAwesome2, Topic 4.16: Recursion (Runestone Academy). Covers the recursive call, why use recursion, the factorial method, base cases, and tracing recursive methods, with a tracing challenge.
- CSAwesome2, 4.55 Recursion Summary (Runestone Academy). Concept summary, vocabulary practice, and common mistakes.
- CSAwesome2, 4.59 Recursion Exercises (Runestone Academy). Base case practice plus easy, medium, and hard multiple-choice questions for self-checking.
- AP Computer Science A Course at a Glance (College Board). Official topic map showing where 4.16 fits in Unit 4.
Submit Assignment
Need to update a submission later? Open the submissions dashboard.