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
It still works, but the program is getting harder and harder to read.
797
797
Newlines and spaces are important for organizing your program visually, making it easier to understand the program and find errors when they occur.
@@ -1661,15 +1661,15 @@ \section{Types of errors}
1661
1661
Error messages from the compiler usually indicate where in the program the error occurred, and sometimes they can tell you exactly what the error is.
1662
1662
As an example, let's get back to the hello world program from Section~\ref{hello}.
1663
1663
1664
-
\begin{code}
1664
+
\begin{trinket}{Hello.java}
1665
1665
public class Hello {
1666
1666
1667
1667
public static void main(String[] args) {
1668
1668
// generate some simple output
1669
1669
System.out.println("Hello, World!");
1670
1670
}
1671
1671
}
1672
-
\end{code}
1672
+
\end{trinket}
1673
1673
1674
1674
\index{semicolon}
1675
1675
@@ -1753,15 +1753,15 @@ \section{Types of errors}
1753
1753
Instead, it will do exactly what you told it to do.
1754
1754
For example, here is a version of the hello world program with a logic error:
1755
1755
1756
-
\begin{code}
1756
+
\begin{trinket}{Hello.java}
1757
1757
public class Hello {
1758
1758
1759
1759
public static void main(String[] args) {
1760
1760
System.out.println("Hello, ");
1761
1761
System.out.println("World!");
1762
1762
}
1763
1763
}
1764
-
\end{code}
1764
+
\end{trinket}
1765
1765
1766
1766
This program compiles and runs just fine, but the output is:
1767
1767
@@ -2068,7 +2068,7 @@ \section{The Scanner class}
2068
2068
\java{Scanner} provides a method called \java{nextLine} that reads a line of input from the keyboard and returns a \java{String}.
2069
2069
The following example reads two lines and repeats them back to the user:
2070
2070
2071
-
\begin{code}
2071
+
\begin{trinket}{Echo.java}
2072
2072
import java.util.Scanner;
2073
2073
2074
2074
public class Echo {
@@ -2086,7 +2086,7 @@ \section{The Scanner class}
2086
2086
System.out.println("You also said: " + line);
2087
2087
}
2088
2088
}
2089
-
\end{code}
2089
+
\end{trinket}
2090
2090
2091
2091
If you omit the import statement and later refer to \java{Scanner}, you will get a compiler error like ``cannot find symbol''.
2092
2092
That means the compiler doesn't know what you mean by \java{Scanner}.
@@ -2397,7 +2397,7 @@ \section{Putting it all together}
2397
2397
%Since we've looked at each of these topics in isolation, it's important to see how they fit together in a complete program.
2398
2398
%If you've been working through the examples on your computer as you've been reading (like we recommended in Section~\ref{sec:examples}), then good job!
2399
2399
2400
-
\begin{code}
2400
+
\begin{trinket}{Convert.java}
2401
2401
import java.util.Scanner;
2402
2402
2403
2403
/**
@@ -2424,7 +2424,7 @@ \section{Putting it all together}
2424
2424
cm, feet, remainder);
2425
2425
}
2426
2426
}
2427
-
\end{code}
2427
+
\end{trinket}
2428
2428
2429
2429
Although not required, all variables and constants are declared at the top of \java{main}.
2430
2430
This practice makes it easier to find their types later on, and it helps the reader know what data is involved in the algorithm.
@@ -2645,7 +2645,7 @@ \section{Exercises}
2645
2645
To choose a random number, you can use the \java{Random} class in \java{java.util}.
2646
2646
Here's how it works:
2647
2647
2648
-
\begin{code}
2648
+
\begin{trinket}{GuessStarter.java}
2649
2649
import java.util.Random;
2650
2650
2651
2651
public class GuessStarter {
@@ -2657,7 +2657,7 @@ \section{Exercises}
2657
2657
System.out.println(number);
2658
2658
}
2659
2659
}
2660
-
\end{code}
2660
+
\end{trinket}
2661
2661
2662
2662
\index{new}
2663
2663
\index{operator!new}
@@ -2833,7 +2833,7 @@ \section{Adding new methods}
2833
2833
You have probably guessed by now that you can define more than one method in a class.
2834
2834
Here's an example:
2835
2835
2836
-
\begin{code}
2836
+
\begin{trinket}{NweLine.java}
2837
2837
public class NewLine {
2838
2838
2839
2839
public static void newLine() {
@@ -2846,7 +2846,7 @@ \section{Adding new methods}
2846
2846
System.out.println("Second line.");
2847
2847
}
2848
2848
}
2849
-
\end{code}
2849
+
\end{trinket}
2850
2850
2851
2851
\index{main}
2852
2852
\index{case-sensitive}
@@ -2948,7 +2948,7 @@ \section{Flow of execution}
2948
2948
2949
2949
Pulling together the code from the previous section, the complete program looks like this:
2950
2950
2951
-
\begin{code}
2951
+
\begin{trinket}{NewLine.java}
2952
2952
public class NewLine {
2953
2953
2954
2954
public static void newLine() {
@@ -2967,7 +2967,7 @@ \section{Flow of execution}
2967
2967
System.out.println("Second line.");
2968
2968
}
2969
2969
}
2970
-
\end{code}
2970
+
\end{trinket}
2971
2971
2972
2972
\index{flow of execution}
2973
2973
@@ -3008,7 +3008,7 @@ \section{Parameters and arguments}
3008
3008
The parameter list indicates what arguments are required.
3009
3009
The following class shows an example:
3010
3010
3011
-
\begin{code}
3011
+
\begin{trinket}{PrintTwice.java}
3012
3012
public class PrintTwice {
3013
3013
3014
3014
public static void printTwice(String s) {
@@ -3020,7 +3020,7 @@ \section{Parameters and arguments}
3020
3020
printTwice("Don't make me say this twice!");
3021
3021
}
3022
3022
}
3023
-
\end{code}
3023
+
\end{trinket}
3024
3024
3025
3025
\java{printTwice} has a parameter named \java{s} with type \java{String}.
3026
3026
When we invoke \java{printTwice}, we have to provide an argument with type \java{String}.
@@ -3128,7 +3128,7 @@ \section{Stack diagrams}
3128
3128
3129
3129
Pulling together the code fragments from the previous section, here is a complete class definition:
3130
3130
3131
-
\begin{code}
3131
+
\begin{trinket}{PrintTime.java}
3132
3132
public class PrintTime {
3133
3133
3134
3134
public static void printTime(int hour, int minute) {
@@ -3143,7 +3143,7 @@ \section{Stack diagrams}
3143
3143
printTime(hour, minute);
3144
3144
}
3145
3145
}
3146
-
\end{code}
3146
+
\end{trinket}
3147
3147
3148
3148
\java{printTime} has two parameters, named \java{hour} and \java{minute}.
3149
3149
And \java{main} has two variables, also named \java{hour} and \java{minute}.
0 commit comments