Skip to content

Commit

Permalink
more improvements to flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Dec 30, 2017
1 parent 8153556 commit e9c8300
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
40 changes: 20 additions & 20 deletions ch04.tex
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,6 @@ \section{Defining new methods}
}
\end{trinket}

Beginners often wonder why it's worth the trouble to write other methods, when they could just do everything in \java{main}.
The \java{NewLine} example demonstrates a few reasons:

\begin{itemize}

\item Creating a new method allows you to {\em name a block of statements}, which makes the code easier to read and understand.
%Methods simplify a program by hiding complex computations behind a single statement, and by using English words in place of arcane code.
%Which is clearer, \java{newLine} or \java{System.out.println()}?

\item Introducing new methods can {\em make the program shorter} by eliminating repetitive code.
For example, to display nine consecutive newlines, you could invoke \java{threeLine} three times.

\item A common problem-solving technique is to {\em break problems down} into sub-problems.
Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution.

\end{itemize}

Perhaps most importantly, organizing your code into multiple methods allows you to test individual parts of your program separately.
It's easier to get a complex program working if you know that each method works correctly.


\section{Flow of execution}

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

Beginners often wonder why it's worth the trouble to write other methods, when they could just do everything in \java{main}.
The \java{NewLine} example demonstrates a few reasons:

\begin{itemize}

\item Creating a new method allows you to {\em name a block of statements}, which makes the code easier to read and understand.
%Methods simplify a program by hiding complex computations behind a single statement, and by using English words in place of arcane code.
%Which is clearer, \java{newLine} or \java{System.out.println()}?

\item Introducing new methods can {\em make the program shorter} by eliminating repetitive code.
For example, to display nine consecutive newlines, you could invoke \java{threeLine} three times.

\item A common problem-solving technique is to {\em break problems down} into sub-problems.
Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution.

\end{itemize}

Perhaps most importantly, organizing your code into multiple methods allows you to test individual parts of your program separately.
It's easier to get a complex program working if you know that each method works correctly.


\section{Parameters and arguments}

Expand Down
24 changes: 12 additions & 12 deletions ch05.tex
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ \section{Relational operators}
When comparing values of different numeric types, Java applies the same conversion rules we saw previously with the assignment operator.
For example, when evaluating the expression \java{5 < 6.0}, Java automatically converts the \java{5} to \java{5.0}.

Most relational operators don't work with strings.
But confusingly, \java{==} and \java{!=} \emph{seem} to work with strings -- they just don't do what you expect.
We'll explain what they do later, but in the meantime, don't use them with strings.
Instead, you should use the \java{equals} method:

\begin{code}
String fruit1 = "Apple";
String fruit2 = "Orange";
System.out.println(fruit1.equals(fruit2));
\end{code}

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


\section{The if-else statement}
Expand Down
32 changes: 18 additions & 14 deletions ch08.tex
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,20 @@ \section{The leap of faith}
When you invoke \java{Math.cos} or \java{System.out.println}, you don't examine or think about the implementations of those methods.
You just assume that they work properly.

For example, this method (from Section~\ref{boolmeth}) determines whether an integer has only one digit.
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.
The same is true of other methods.
For example, consider the method from Section~\ref{boolmeth} that determines whether an integer has only one digit:

\begin{code}
public static boolean isSingleDigit(int x) {
return x > -10 && x < 10;
}
\end{code}

The same is true of recursive methods.
When you get to the recursive call, instead of following the flow of execution you should {\em assume} that the recursive invocation works.
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.

Recursive methods are no different.
When you get to a recursive call, don't try to follow the flow of execution.
Instead, you should {\em assume} that the recursive call produces the desired result.

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


\section{Fibonacci sequence}
%\section{Fibonacci sequence}
\label{fibonacci}

\index{fibonacci}
Expand Down Expand Up @@ -696,6 +699,15 @@ \section{Exercises}
\end{exercise}


\begin{exercise} %%V6 Ex6.7

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.
Start with a base case, and use temporary variables to debug your solution.
You might find it helpful to print the value of \java{n} each time \java{oddSum} is invoked.

\end{exercise}


\begin{exercise} %%V6 Ex6.6

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


\begin{exercise} %%V6 Ex6.7

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.
Start with a base case, and use temporary variables to debug your solution.
You might find it helpful to print the value of \java{n} each time \java{oddSum} is invoked.

\end{exercise}


\begin{exercise} %%V6 Ex6.8

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


\newpage
\begin{exercise} %%V6 Ex9.4

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

0 comments on commit e9c8300

Please sign in to comment.