Skip to content

Commit 8c2e1f5

Browse files
committed
added linking to text
1 parent 0dcbed7 commit 8c2e1f5

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ void main() {
524524
<title>List</title>
525525

526526
<p>
527-
Next, let&#x2019;s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. Here is a simple Python program that creates and prints a histogram.
527+
Next, let&#x2019;s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. <xref ref="python-histogram" text="type-global"/> is a simple Python program that creates and prints a histogram.
528528
</p>
529529

530-
<listing xml:id="python-input-file1">
530+
<listing xml:id="python-histogram">
531531
<program interactive="activecode" language="python">
532532
<code>
533533
def main():
@@ -545,17 +545,18 @@ main()
545545
</listing>
546546

547547
<p>
548-
Test running the program. It will read this data:
548+
Test running the program. It will read this data:
549549
</p>
550550

551-
<datafile xmlid="datafile-test" label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
551+
<datafile label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
552552
<pre>
553553
1
554554
2
555555
3
556556
9
557557
1
558-
</pre> </datafile>
558+
</pre>
559+
</datafile>
559560

560561
<p>
561562
Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called &#x2018;test.dat&#x2019;. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position.
@@ -618,7 +619,7 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
618619
</p>
619620

620621
<p>
621-
Here is the Java code needed to write the exact same program:
622+
<xref ref="java-histogram" text="type-global"/> is the Java code needed to write the exact same program.
622623
</p>
623624

624625
<note>
@@ -627,7 +628,7 @@ Here is the Java code needed to write the exact same program:
627628
</p>
628629
</note>
629630

630-
<listing xml:id="java-null-object">
631+
<listing xml:id="java-histogram">
631632
<program interactive="activecode" language="java" datafile="test.dat">
632633
<code>
633634
import java.util.Scanner;
@@ -667,15 +668,15 @@ public class Histo {
667668
</listing>
668669

669670
<p>
670-
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
671+
Before going any further, I suggest you try to compile <xref ref="java-histogram" text="type-global"/> and run it on some test data that you create.
671672
</p>
672673

673674
<p>
674675
Now, let&#x2019;s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In this example we are declaring a <c>Scanner</c> variable called <c>data</c>, an integer called <c>idx</c> and an <c>ArrayList</c> called <c>count</c>. However, there is a new twist to the <c>ArrayList</c> declaration. Unlike Python where lists can contain just about anything, in Java we let the compiler know what kind of objects our array list is going to contain. In this case the <c>ArrayList</c> will contain <c>Integers</c>. The syntax we use to declare what kind of object the list will contain is the <c>&lt;Type&gt;</c> syntax.
675676
</p>
676677

677678
<p>
678-
Technically, you don&#x2019;t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c>&lt;``*Type*</c>&gt;`` off the declaration. If you don&#x2019;t tell Java what kind of object is going to be on the list Java will give you a warning message like this:
679+
Technically, you don&#x2019;t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c>&lt;``*Type*</c>&gt;`` off the declaration. If you don&#x2019;t tell Java what kind of object is going to be on the list Java will give you a warning message like <xref ref="java-arraylist-warning" text="type-global"/> when you compile the program.
679680
</p>
680681

681682
<listing xml:id="java-arraylist-warning">
@@ -692,7 +693,7 @@ public class Histo {
692693
</p>
693694

694695
<p>
695-
Lines 13&#x2014;20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. The following example shows the general structure of a try/catch block.
696+
Lines 13&#x2014;20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. <xref ref="java-try-catch" text="type-global"/> shows the general structure of a try/catch block.
696697
</p>
697698

698699
<listing xml:id="java-try-catch">

0 commit comments

Comments
 (0)