📖 Scenario: Imagine you have a stack of boxes, and you want to count how many boxes are in the stack. You can only look at the top box and then the rest of the stack below it. This is like a simple problem where recursion helps by breaking it down step-by-step.
🎯 Goal: You will write a recursive Java method to count the number of boxes in a stack represented by an integer. You will practice writing the base case and the recursive case clearly.
📋 What You'll Learn
Create a method called countBoxes that takes an integer n representing the number of boxes.
Write the base case for when there are no boxes left (n = 0).
Write the recursive case that calls countBoxes with one less box.
Print the total count of boxes returned by the recursive method.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like navigating folders, solving puzzles, and processing data structures like trees.
💼 Career
Understanding recursion is important for software development roles that involve algorithms, problem solving, and working with complex data structures.
Progress0 / 4 steps
1
Create the initial number of boxes
Create an integer variable called boxes and set it to 5 to represent the number of boxes in the stack.
DSA Java
Hint
Use int boxes = 5; to create the variable.
2
Add the recursive method signature
Add a public static method called countBoxes that takes an integer parameter n and returns an integer.
DSA Java
Hint
Write the method header as public static int countBoxes(int n).
3
Write the base case and recursive case
Inside the countBoxes method, write the base case: if n == 0, return 0. Then write the recursive case: return 1 + countBoxes(n - 1).
DSA Java
Hint
The base case stops recursion when n is zero. The recursive case counts one box and calls itself with one less box.
4
Print the result of the recursive count
In the main method, add a print statement that calls countBoxes(boxes) and prints the result.
DSA Java
Hint
Use System.out.println(countBoxes(boxes)); to print the result.
Practice
(1/5)
1. In recursion, what is the purpose of the base case?
easy
A. To print the result of the recursion
B. To call the function again with a smaller input
C. To increase the input size for the next call
D. To stop the recursion and prevent infinite calls
Solution
Step 1: Understand the base case role
The base case is the condition where recursion stops to avoid infinite calls.
Step 2: Differentiate base case from recursive case
The recursive case calls the function again, but the base case ends recursion.
Final Answer:
To stop the recursion and prevent infinite calls -> Option D
Quick Check:
Base case stops recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls function again
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in a recursive Java method that calculates factorial?
easy
A. if (n > 0) return n * factorial(n - 1);
B. return n * factorial(n + 1);
C. if (n == 0) return 1;
D. if (n == 1) factorial(n);
Solution
Step 1: Identify base case condition for factorial
Factorial base case is when n equals 0, return 1.
Step 2: Check syntax correctness
if (n == 0) return 1; correctly returns 1 when n == 0, stopping recursion.
Final Answer:
if (n == 0) return 1; -> Option C
Quick Check:
Base case for factorial is n == 0 [OK]
Hint: Base case returns fixed value, usually simplest input [OK]
Common Mistakes:
Using wrong condition for base case
Calling factorial with n + 1 causing infinite recursion
Not returning a value in base case
3. What is the output of this Java code?
public class Test {
public static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
public static void main(String[] args) {
System.out.println(sum(3));
}
}