From 8153556b5dc61739948be758a3bc93fd4c0e7cda Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Sat, 30 Dec 2017 10:36:42 -0500 Subject: [PATCH] fix spacing, add details --- ch04.tex | 27 +++++++++++++-------------- ch05.tex | 27 +++++++++++++++------------ ch08.tex | 4 +--- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/ch04.tex b/ch04.tex index baa8d1e..7ca40bc 100644 --- a/ch04.tex +++ b/ch04.tex @@ -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} @@ -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 { @@ -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} @@ -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); @@ -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. diff --git a/ch05.tex b/ch05.tex index 2eae566..a9d8180 100644 --- a/ch05.tex +++ b/ch05.tex @@ -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} @@ -388,6 +388,7 @@ \section{Boolean variables} \section{Boolean methods} +\label{boolmeth} \index{boolean} \index{method!boolean} @@ -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: diff --git a/ch08.tex b/ch08.tex index c405c54..31a3dfe 100644 --- a/ch08.tex +++ b/ch08.tex @@ -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}