You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch7_recursion.ptx
+8-11Lines changed: 8 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -324,14 +324,14 @@ public class ArrayProcessor {
324
324
public static void main(String[] args) {
325
325
System.out.println("Calling the recursive method... this will end in an error!");
326
326
327
-
causeStackOverflow();
327
+
causeStackOverflow();
328
328
}
329
329
}
330
330
</code>
331
331
</program>
332
332
</section>
333
333
334
-
<sectionxml:id="recursion_java_summary">
334
+
<sectionxml:id="recursion_summary">
335
335
<title>Summary & Reading Questions</title>
336
336
<p><ollabel="1">
337
337
<li>
@@ -344,23 +344,20 @@ public class ArrayProcessor {
344
344
<p>The recursive logic in Java mirrors Python conceptually, but Java uses curly braces <c>{}</c> and explicit types instead of indentation and dynamic typing.</p>
345
345
</li>
346
346
<li>
347
-
<p>The helper-method pattern keeps public APIs clean (e.g., <c>sumArray(int[] arr)</c>) while a private helper (e.g., <c>sumHelper(int[] arr, int index)</c>) carries extra state like the current index.</p>
348
-
</li>
349
-
<li>
350
-
<p>Closing over array bounds and indexes in the helper avoids forcing callers to provide implementation details (like a starting index).</p>
347
+
<p>The helper method pattern hides implementation details (like array indices) from callers, providing clean public interfaces while managing recursive state privately.</p>
351
348
</li>
352
349
<li>
353
350
<p>Deep or unbounded recursion can exhaust the call stack: Python raises <c>RecursionError</c>; Java throws <c>StackOverflowError</c>.</p>
354
351
</li>
355
352
<li>
356
-
<p>Neither Java nor Python guarantees tail call optimization; prefer iterative solutions for algorithms requiring very deep recursion.</p>
353
+
<p>Neither Java nor Python guarantees tail call optimization, so programmers should use iterative solutions for algorithms that would require very deep recursion.</p>
357
354
</li>
358
355
<li>
359
-
<p>Error signaling differs across languages; for example, a Java factorial that receives a negative <c>n</c> might return a sentinel value (e.g., <c>-1</c>) after printing an error message.</p>
356
+
<p>Recursive methods in Java must specify return types explicitly, unlike Python's dynamic typing, which affects how you handle error cases and return values.</p>
360
357
</li>
361
358
</ol></p>
362
359
<reading-questionsxml:id="rqs-recursion-java">
363
-
<exerciselabel="recursion-1">
360
+
<exerciselabel="recursion-method-signature">
364
361
<statement>
365
362
<p>Which method signature and behavior best match a typical Java recursive factorial implementation?</p>
366
363
</statement>
@@ -383,7 +380,7 @@ public class ArrayProcessor {
383
380
</choice>
384
381
</choices>
385
382
</exercise>
386
-
<exerciselabel="recursion-2">
383
+
<exerciselabel="recursion-helper-method">
387
384
<statement>
388
385
<p>Why use a private helper method (e.g., <c>sumHelper(int[] arr, int index)</c>) behind a public method (e.g., <c>sumArray(int[] arr)</c>) in recursive array processing?</p>
389
386
</statement>
@@ -406,7 +403,7 @@ public class ArrayProcessor {
406
403
</choice>
407
404
</choices>
408
405
</exercise>
409
-
<exerciselabel="recursion-3">
406
+
<exerciselabel="recursion-limits">
410
407
<statement>
411
408
<p>Which statement about recursion limits and errors is accurate?</p>
0 commit comments