Skip to content

Commit

Permalink
fix spacing, add details
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Dec 30, 2017
1 parent 06298d5 commit 8153556
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
27 changes: 13 additions & 14 deletions ch04.tex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ \chapter{Methods and testing}

\section{Math methods}

In the next two sections, we'll take a break from conditions and logic and discuss other areas of mathematics in Java.
%In the next two sections, we'll take a break from conditions and logic and discuss other areas of mathematics in Java.

\index{Math class}
\index{class!Math}
Expand Down Expand Up @@ -190,10 +190,13 @@ \section{Defining new methods}

Notice the extra space between the lines.
If we wanted more space between them, we could invoke the same method repeatedly.

%Or we could write yet another method (named \java{threeLine}) that displays three blank lines:
Or we could write yet another method (named \java{threeLine}) that displays three blank lines.
%Pulling together the code from the previous section, the complete program looks like this:

In the following program, \java{main} invokes \java{threeLine}, and \java{threeLine} invokes \java{newLine} three times.
%Since \java{newLine} has no parameters, it requires no arguments, as shown when it is invoked in \java{main}.
Because \java{newLine} is in the same class as \java{threeLine}, we don't have to specify the class name like \java{NewLine.newLine()}.

\begin{trinket}{NewLine.java}
public class NewLine {

Expand All @@ -215,11 +218,7 @@ \section{Defining new methods}
}
\end{trinket}

In this example, \java{main} invokes \java{threeLine}, and \java{threeLine} invokes \java{newLine} three times.
%Since \java{newLine} has no parameters, it requires no arguments, as shown when it is invoked in \java{main}.
Because \java{newLine} is in the same class as \java{threeLine}, we don't have to specify the class name like \java{NewLine.newLine()}.

Beginners wonder why it's worth the trouble to write other methods, when they could just do everything in \java{main}.
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}
Expand Down Expand Up @@ -790,6 +789,12 @@ \section{Exercises}
This exercise reviews the flow of execution through a program with multiple methods.
Read the following code and answer the questions.

\begin{code}
public static void main(String[] args) {
zippo("rattle", 13);
}
\end{code}

\begin{code}
public static void baffle(String blimp) {
System.out.println(blimp);
Expand All @@ -809,12 +814,6 @@ \section{Exercises}
}
\end{code}

\begin{code}
public static void main(String[] args) {
zippo("rattle", 13);
}
\end{code}

\begin{enumerate}

\item Write the number {\tt 1} next to the first line of code in this program that will execute.
Expand Down
27 changes: 15 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{!=} do work with strings -- they just don't do what you expect.
%We'll explain what they do later; 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 Expand Up @@ -388,6 +388,7 @@ \section{Boolean variables}


\section{Boolean methods}
\label{boolmeth}

\index{boolean}
\index{method!boolean}
Expand Down Expand Up @@ -450,7 +451,9 @@ \section{Validating input}

One of the most important tasks in any computer program is to {\bf validate} input from the user.
People often make mistakes while typing, especially on smartphones, and incorrect inputs may cause your program to fail.

Even worse, someone (i.e., a {\bf hacker}) may intentionally try to break into your system by entering unexpected inputs.
You should never assume that users will input the right kind of data.

Consider this simple program that prompts the user for a number and computes its logarithm:

Expand Down
4 changes: 1 addition & 3 deletions ch08.tex
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ \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.

%TODO reference appendix B instead? or use a different example?

For example, this method (from Appendix~\ref{extras}) determines whether an integer has only one digit.
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.

\begin{code}
Expand Down

0 comments on commit 8153556

Please sign in to comment.