Skip to content

Commit 1bcd696

Browse files
authored
Merge pull request #235 from habibasorour/issue_227_list
Issue 227 fixed: 3.4 listing
2 parents 951fb9a + bf922d3 commit 1bcd696

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ 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-
531-
<program xml:id="python-input-file1" interactive="activecode" language="python">
530+
<listing xml:id="python-histogram">
531+
<program interactive="activecode" language="python" add-files="datafile-test">
532532
<code>
533533
def main():
534534
count = [0]*10
@@ -542,25 +542,31 @@ def main():
542542
main()
543543
</code> <tests> </tests>
544544
</program>
545+
</listing>
545546

546547
<p>
547-
Test running the program. It will read this data:
548+
Test running the program. It will read <xref ref="datafile-test" text="type-global"/>.
548549
</p>
550+
551+
<data xml:id="datafile-test">
552+
<title>Data file for testing the histogram program</title>
549553
<datafile label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
550554
<pre>
551555
1
552556
2
553557
3
554558
9
555559
1
556-
</pre> </datafile>
560+
</pre>
561+
</datafile>
562+
</data>
557563

558564
<p>
559565
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.
560566
</p>
561567

562568
<p>
563-
To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, called an <c>ArrayList.</c> Next, you will see three different kinds of loops used in Java. Two of the loops we will use are going to be very familiar, the third one is different from what you are used to in Python but is easy when you understand the syntax:
569+
To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, an <c>ArrayList</c>. Next, you will see three different kinds of loops used in Java. Two of the loops we will use are going to be very familiar, the third one is different from what you are used to in Python but is easy when you understand the syntax:
564570
</p>
565571

566572
<p>
@@ -616,7 +622,7 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
616622
</p>
617623

618624
<p>
619-
Here is the Java code needed to write the exact same program:
625+
<xref ref="java-histogram" text="type-global"/> is the Java code needed to write the exact same program.
620626
</p>
621627

622628
<note>
@@ -625,7 +631,8 @@ Here is the Java code needed to write the exact same program:
625631
</p>
626632
</note>
627633

628-
<program xml:id="java-null-object" interactive="activecode" language="java" datafile="test.dat">
634+
<listing xml:id="java-histogram">
635+
<program interactive="activecode" language="java" datafile="test.dat">
629636
<code>
630637
import java.util.Scanner;
631638
import java.util.ArrayList;
@@ -661,39 +668,48 @@ public class Histo {
661668
}
662669
</code> <tests> </tests>
663670
</program>
671+
</listing>
664672

665673
<p>
666-
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
674+
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.
667675
</p>
668676

669677
<p>
670678
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.
671679
</p>
672680

673681
<p>
674-
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:
682+
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.
675683
</p>
676684

677-
<pre>
685+
<listing xml:id="java-arraylist-warning">
686+
<program>
687+
<code>
678688
Note: Histo.java uses unchecked or unsafe operations.
679689
Note: Recompile with -Xlint:unchecked for details.
680-
</pre>
690+
</code>
691+
</program>
692+
</listing>
681693

682694
<p>
683695
Without the <c>&lt;Integer&gt;</c> part of the declaration Java simply assumes that <em>any</em> object can be on the list. However, without resorting to an ugly notation called casting, you cannot do anything with the objects on a list like this! So, if you forget you will surely see more errors later in your code. (Try it and see what you get)
684696
</p>
685697

686698
<p>
687-
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.
699+
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.
688700
</p>
689-
690-
<pre>
701+
702+
<listing xml:id="java-try-catch">
703+
<program>
704+
<code>
691705
try {
692706
Put some risky code in here, like opening a file
693707
} catch (Exception e) {
694708
If an error happens in the try block an exception is thrown. We will catch that exception here!
695709
}
696-
</pre>
710+
</code>
711+
</program>
712+
</listing>
697713

698714
<p>
699715
Notice that in line 16 we are catching an <c>IOException</c>. In fact, we will see later that we can have multiple catch blocks to catch different types of exceptions. If we want to be lazy and catch any old exception we can catch an <c>Exception</c> which is the parent of all exceptions. However, catching <c>Exception</c> is a terrible practice, since you may inadvertently catch exceptions you do not intend to, making it harder to identify bugs in your program.

0 commit comments

Comments
 (0)