Skip to content

Commit e9c8300

Browse files
committed
more improvements to flow
1 parent 8153556 commit e9c8300

3 files changed

Lines changed: 50 additions & 46 deletions

File tree

ch04.tex

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,26 +218,6 @@ \section{Defining new methods}
218218
}
219219
\end{trinket}
220220

221-
Beginners often wonder why it's worth the trouble to write other methods, when they could just do everything in \java{main}.
222-
The \java{NewLine} example demonstrates a few reasons:
223-
224-
\begin{itemize}
225-
226-
\item Creating a new method allows you to {\em name a block of statements}, which makes the code easier to read and understand.
227-
%Methods simplify a program by hiding complex computations behind a single statement, and by using English words in place of arcane code.
228-
%Which is clearer, \java{newLine} or \java{System.out.println()}?
229-
230-
\item Introducing new methods can {\em make the program shorter} by eliminating repetitive code.
231-
For example, to display nine consecutive newlines, you could invoke \java{threeLine} three times.
232-
233-
\item A common problem-solving technique is to {\em break problems down} into sub-problems.
234-
Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution.
235-
236-
\end{itemize}
237-
238-
Perhaps most importantly, organizing your code into multiple methods allows you to test individual parts of your program separately.
239-
It's easier to get a complex program working if you know that each method works correctly.
240-
241221

242222
\section{Flow of execution}
243223

@@ -266,6 +246,26 @@ \section{Flow of execution}
266246
%Instead, execution picks up where it left off in the program that invoked \java{main}, which is the Java interpreter.
267247
%The interpreter takes care of things like deleting windows and general cleanup, and {\em then} the program terminates.
268248

249+
Beginners often wonder why it's worth the trouble to write other methods, when they could just do everything in \java{main}.
250+
The \java{NewLine} example demonstrates a few reasons:
251+
252+
\begin{itemize}
253+
254+
\item Creating a new method allows you to {\em name a block of statements}, which makes the code easier to read and understand.
255+
%Methods simplify a program by hiding complex computations behind a single statement, and by using English words in place of arcane code.
256+
%Which is clearer, \java{newLine} or \java{System.out.println()}?
257+
258+
\item Introducing new methods can {\em make the program shorter} by eliminating repetitive code.
259+
For example, to display nine consecutive newlines, you could invoke \java{threeLine} three times.
260+
261+
\item A common problem-solving technique is to {\em break problems down} into sub-problems.
262+
Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution.
263+
264+
\end{itemize}
265+
266+
Perhaps most importantly, organizing your code into multiple methods allows you to test individual parts of your program separately.
267+
It's easier to get a complex program working if you know that each method works correctly.
268+
269269

270270
\section{Parameters and arguments}
271271

ch05.tex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ \section{Relational operators}
4747
When comparing values of different numeric types, Java applies the same conversion rules we saw previously with the assignment operator.
4848
For example, when evaluating the expression \java{5 < 6.0}, Java automatically converts the \java{5} to \java{5.0}.
4949

50-
Most relational operators don't work with strings.
51-
But confusingly, \java{==} and \java{!=} \emph{seem} to work with strings -- they just don't do what you expect.
52-
We'll explain what they do later, but in the meantime, don't use them with strings.
53-
Instead, you should use the \java{equals} method:
54-
55-
\begin{code}
56-
String fruit1 = "Apple";
57-
String fruit2 = "Orange";
58-
System.out.println(fruit1.equals(fruit2));
59-
\end{code}
60-
61-
The result of \java{fruit1.equals(fruit2)} is the boolean value \java{false}.
50+
%Most relational operators don't work with strings.
51+
%But confusingly, \java{==} and \java{!=} \emph{seem} to work with strings -- they just don't do what you expect.
52+
%We'll explain what they do later, but in the meantime, don't use them with strings.
53+
%Instead, you should use the \java{equals} method:
54+
%
55+
%\begin{code}
56+
%String fruit1 = "Apple";
57+
%String fruit2 = "Orange";
58+
%System.out.println(fruit1.equals(fruit2));
59+
%\end{code}
60+
%
61+
%The result of \java{fruit1.equals(fruit2)} is the boolean value \java{false}.
6262

6363

6464
\section{The if-else statement}

ch08.tex

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,20 @@ \section{The leap of faith}
286286
When you invoke \java{Math.cos} or \java{System.out.println}, you don't examine or think about the implementations of those methods.
287287
You just assume that they work properly.
288288

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:
291291

292292
\begin{code}
293293
public static boolean isSingleDigit(int x) {
294294
return x > -10 && x < 10;
295295
}
296296
\end{code}
297297

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.
300303

301304
For example, ``Assuming that I can find the factorial of $n-1$, can I compute the factorial of $n$?''
302305
Yes you can, by multiplying by $n$.
@@ -322,7 +325,7 @@ \section{The leap of faith}
322325
But that's why it's called the leap of faith!
323326

324327

325-
\section{Fibonacci sequence}
328+
%\section{Fibonacci sequence}
326329
\label{fibonacci}
327330

328331
\index{fibonacci}
@@ -696,6 +699,15 @@ \section{Exercises}
696699
\end{exercise}
697700

698701

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+
699711
\begin{exercise} %%V6 Ex6.6
700712

701713
In this exercise, you will use a stack diagram to understand the execution of the following recursive method.
@@ -733,15 +745,6 @@ \section{Exercises}
733745
\end{exercise}
734746

735747

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-
745748
\begin{exercise} %%V6 Ex6.8
746749

747750
The goal of this exercise is to translate a recursive definition into a Java method.
@@ -805,6 +808,7 @@ \section{Exercises}
805808
\end{exercise}
806809

807810

811+
\newpage
808812
\begin{exercise} %%V6 Ex9.4
809813

810814
Create a program called {\tt Recurse.java} and type in the following methods:

0 commit comments

Comments
 (0)