From 0591caa8774b50c5264dbf91972cd28943147779 Mon Sep 17 00:00:00 2001 From: "nshizirungudieumerci@gmail.com" Date: Mon, 10 Aug 2026 14:48:29 -0400 Subject: [PATCH] added a recursion parsons problem --- source/ch7_recursion.ptx | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx index f04f71c..2286752 100644 --- a/source/ch7_recursion.ptx +++ b/source/ch7_recursion.ptx @@ -125,6 +125,46 @@ public class MTools {

Notice the key differences from Python: instead of def factorial(n):, Java uses public static int factorial(int n) which declares the method's visibility as public, that it belongs to the class rather than an instance (hence, static), the return type as integer, and the parameter type also as integer. The recursive logic—base case and recursive step—remains identical to Python, and, of course, all code blocks use curly braces {} instead of indentation.

+ + + +

+ Construct a recursive Java method that computes the factorial of a number. + Drag the blocks into the correct order on the right. +

+
+ + + public static int factorial(int n) { + + + + + if (n <= 1) { + return 1; + } + + + if (n <= 1) { + return 0; + } + + + + + + return n * factorial(n - 1); + + + return n * factorial(n); + + + + + } + + +