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: ch08.tex
+18-14Lines changed: 18 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -286,17 +286,20 @@ \section{The leap of faith}
286
286
When you invoke \java{Math.cos} or \java{System.out.println}, you don't examine or think about the implementations of those methods.
287
287
You just assume that they work properly.
288
288
289
-
For example, this method (from Section~\ref{boolmeth}) determines whether an integer has only one digit.
290
-
Once you convince yourself that this method is correct -- by testing and examination of the code -- you can use the method without ever looking at the implementation again.
289
+
The same is true of other methods.
290
+
For example, consider the method from Section~\ref{boolmeth} that determines whether an integer has only one digit:
291
291
292
292
\begin{code}
293
293
public static boolean isSingleDigit(int x) {
294
294
return x > -10 && x < 10;
295
295
}
296
296
\end{code}
297
297
298
-
The same is true of recursive methods.
299
-
When you get to the recursive call, instead of following the flow of execution you should {\em assume} that the recursive invocation works.
298
+
Once you convince yourself that this method is correct -- by examining and testing the code -- you can just use the method without ever looking at the implementation again.
299
+
300
+
Recursive methods are no different.
301
+
When you get to a recursive call, don't try to follow the flow of execution.
302
+
Instead, you should {\em assume} that the recursive call produces the desired result.
300
303
301
304
For example, ``Assuming that I can find the factorial of $n-1$, can I compute the factorial of $n$?''
302
305
Yes you can, by multiplying by $n$.
@@ -322,7 +325,7 @@ \section{The leap of faith}
322
325
But that's why it's called the leap of faith!
323
326
324
327
325
-
\section{Fibonacci sequence}
328
+
%\section{Fibonacci sequence}
326
329
\label{fibonacci}
327
330
328
331
\index{fibonacci}
@@ -696,6 +699,15 @@ \section{Exercises}
696
699
\end{exercise}
697
700
698
701
702
+
\begin{exercise} %%V6 Ex6.7
703
+
704
+
Write a recursive method named \java{oddSum} that takes a positive odd integer \java{n} and returns the sum of odd integers from 1 to n.
705
+
Start with a base case, and use temporary variables to debug your solution.
706
+
You might find it helpful to print the value of \java{n} each time \java{oddSum} is invoked.
707
+
708
+
\end{exercise}
709
+
710
+
699
711
\begin{exercise} %%V6 Ex6.6
700
712
701
713
In this exercise, you will use a stack diagram to understand the execution of the following recursive method.
@@ -733,15 +745,6 @@ \section{Exercises}
733
745
\end{exercise}
734
746
735
747
736
-
\begin{exercise} %%V6 Ex6.7
737
-
738
-
Write a recursive method named \java{oddSum} that takes a positive odd integer \java{n} and returns the sum of odd integers from 1 to n.
739
-
Start with a base case, and use temporary variables to debug your solution.
740
-
You might find it helpful to print the value of \java{n} each time \java{oddSum} is invoked.
741
-
742
-
\end{exercise}
743
-
744
-
745
748
\begin{exercise} %%V6 Ex6.8
746
749
747
750
The goal of this exercise is to translate a recursive definition into a Java method.
@@ -805,6 +808,7 @@ \section{Exercises}
805
808
\end{exercise}
806
809
807
810
811
+
\newpage
808
812
\begin{exercise} %%V6 Ex9.4
809
813
810
814
Create a program called {\tt Recurse.java} and type in the following methods:
0 commit comments