Skip to content

Commit af81531

Browse files
committed
small improvements to 7.1 & 7.2
1 parent 5baad59 commit af81531

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<section xml:id="basic-recursion">
88
<title>Basic Recursion</title>
99
<p>
10-
In this chapter, we will explore how to translate your recursive logic from Python to Java. While the core concepts of recursion remain the same, the syntax and structure of your code will change somewhat.
10+
In this chapter, we will explore how to translate your recursive logic from Python to Java. While the core concepts of recursion remain the same, the syntax and a bit of the structure of your code will change somewhat.
1111
</p>
12-
<p><idx>recursion</idx>
13-
As you may know from Python, <term>recursion</term> is a powerful problem-solving technique involving base cases and recursive steps in which a function or method calls itself. When moving to Java, the core logic you've learned remains identical. The challenge is adapting that logic to Java's statically-typed, class-based syntax.
12+
<p><idx>recursion</idx><idx>base case</idx><idx>recursive step</idx>
13+
As you may know from Python, <term>recursion</term> is a powerful problem-solving technique involving one or more <term>base cases</term> and <term>recursive steps</term> in which a function or method calls itself while moving towards a base case. When moving to Java, the core logic you've learned remains identical. The challenge is adapting that logic to Java's statically-typed, class-based syntax.
1414
</p>
1515

1616
<p>
@@ -34,7 +34,7 @@
3434
as <m>n \times (n-1)!</m>.
3535
</p>
3636
<p>
37-
Here is a Python implementation of factorial using functions:
37+
Here is a Python implementation of factorial using just one function:
3838
</p>
3939
<program xml:id="factorial-python-function" interactive="activecode" language="python">
4040
<code>
@@ -49,20 +49,18 @@ def factorial(n):
4949
# Recursive Step: n * (n-1)!
5050
return n * factorial(n - 1)
5151

52-
def main():
53-
number = 5
54-
print(str(number) + "! is " + str(factorial(number)))
52+
number = 5
53+
print(str(number) + "! is " + str(factorial(number)))
5554

56-
main()
57-
</code>
55+
</code>
5856
</program>
5957

6058
<p>
61-
Many Python programs organize related functions into classes. The same factorial function can be placed inside a class as a method. Then you need to create an instance of the class to call the method. There we create the class <c>MathTools</c> with a method <c>factorial</c>, and we call it from the <c>main</c> function.
59+
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.
6260
</p>
6361
<program xml:id="factorial-python-class" interactive="activecode" language="python">
6462
<code>
65-
class MathTools:
63+
class MTools:
6664
def factorial(self, n):
6765
# Check for negative numbers
6866
if n &lt; 0:
@@ -76,9 +74,9 @@ class MathTools:
7674

7775
def main():
7876
# Create an instance of the class and call the method
79-
math_tools = MathTools()
77+
mtools_instance = MTools()
8078
number = 5
81-
print(str(number) + "! is " + str(math_tools.factorial(number)))
79+
print(str(number) + "! is " + str(mtools_instance.factorial(number)))
8280

8381
main()
8482
</code>
@@ -92,7 +90,7 @@ main()
9290
</p>
9391
<program xml:id="factorial-java-class" interactive="activecode" language="java">
9492
<code>
95-
public class MathTools {
93+
public class MTools {
9694
public static int factorial(int n) {
9795
// Check for negative numbers
9896
if (n &lt; 0) {
@@ -115,7 +113,7 @@ public class MathTools {
115113
</code>
116114
</program>
117115
<p>
118-
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, but all code blocks use curly braces <c>{}</c> instead of indentation.
116+
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.
119117
</p>
120118
</section>
121119

0 commit comments

Comments
 (0)