Skip to content

Commit 9c921e0

Browse files
authored
Merge pull request #339 from habibasorour/issue_326_listing
Issue 326 fixed listing in 7.1
2 parents e57b2e7 + 5d01328 commit 9c921e0

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
as <md>n \times (n-1)!</md>.
3535
</p>
3636
<p>
37-
Here is a Python implementation of factorial using just one function:
37+
<xref ref="factorial-python-function" text="type-global"/> is the Python implementation of the factorial function. It checks for negative numbers, defines the base case for 0! and 1!, and implements the recursive step.
3838
</p>
39-
<program xml:id="factorial-python-function" interactive="activecode" language="python">
39+
<listing xml:id="factorial-python-function">
40+
<program interactive="activecode" language="python">
4041
<code>
4142
def factorial(n):
4243
# Check for negative numbers
@@ -54,11 +55,17 @@ print(str(number) + "! is " + str(factorial(number)))
5455

5556
</code>
5657
</program>
58+
</listing>
5759

5860
<p>
5961
Many Python programs organize related functions into classes. The same factorial function can be placed inside a class as a method instead of as a function. When this is done, you need to create an instance of the class in order to call the method. Below, we create the class <c>MathTools</c> with a method <c>factorial</c>, and we call it from the <c>main</c> function.
6062
</p>
61-
<program xml:id="factorial-python-class" interactive="activecode" language="python">
63+
<p>
64+
<xref ref="factorial-python-class" text="type-global"/> is the Python implementation of the factorial function as a method within a class. It maintains the same logic as the previous function but is now encapsulated within a class structure.
65+
</p>
66+
67+
<listing xml:id="factorial-python-class">
68+
<program interactive="activecode" language="python">
6269
<code>
6370
class MTools:
6471
def factorial(self, n):
@@ -81,14 +88,16 @@ def main():
8188
main()
8289
</code>
8390
</program>
91+
</listing>
8492

8593
<p>
8694
See if you can spot the differences in the Java version below.
8795
</p>
8896
<p>
89-
Here is the equivalent Java code:
97+
<xref ref="factorial-java-class" text="type-global"/> is the Java implementation of the factorial function. It follows the same logic as the Python version but adapts to Java's syntax and type system.
9098
</p>
91-
<program xml:id="factorial-java-class" interactive="activecode" language="java">
99+
<listing xml:id="factorial-java-class">
100+
<program interactive="activecode" language="java">
92101
<code>
93102
public class MTools {
94103
public static int factorial(int n) {
@@ -112,6 +121,7 @@ public class MTools {
112121
}
113122
</code>
114123
</program>
124+
</listing>
115125
<p>
116126
Notice the key differences from Python: instead of <c>def factorial(n):</c>, Java uses <c>public static int factorial(int n)</c> which declares the method's visibility as <c>public</c>, that it belongs to the class rather than an instance (hence, <c>static</c>), 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 <c>{}</c> instead of indentation.
117127
</p>

0 commit comments

Comments
 (0)