Skip to content

Commit ec32254

Browse files
authored
Merge pull request #236 from habibasorour/issue_228_comment
Issue 228 fix: comments in codeblocks in 3.4
2 parents 1bcd696 + 64a6356 commit ec32254

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,14 @@ void main() {
531531
<program interactive="activecode" language="python" add-files="datafile-test">
532532
<code>
533533
def main():
534-
count = [0]*10
535-
data = open('test.dat')
534+
""" Program to read numbers from a file and produce a histogram. """
535+
count = [0]*10 # create a list of 10 zeros
536+
data = open('test.dat') # open the data file
537+
# read each line and update the count
536538
for line in data:
537539
count[int(line)] = count[int(line)] + 1
538540
idx = 0
539-
for num in count:
541+
for num in count: # iterate over the list and print the histogram
540542
print(idx, " occurred ", num, " times.")
541543
idx += 1
542544
main()
@@ -634,33 +636,40 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
634636
<listing xml:id="java-histogram">
635637
<program interactive="activecode" language="java" datafile="test.dat">
636638
<code>
637-
import java.util.Scanner;
638-
import java.util.ArrayList;
639-
import java.io.File;
640-
import java.io.IOException;
639+
import java.util.Scanner; // import the Scanner class to read input from the user
640+
import java.util.ArrayList; // import the ArrayList class to use dynamic arrays
641+
import java.io.File; // import the File class to read from files
642+
import java.io.IOException; // import the IOException class to handle file input/output exceptions
643+
/**
644+
* Program to read numbers from a file and produce a histogram in Java.
645+
*/
641646
public class Histo {
642647
public static void main(String[] args) {
643-
Scanner data = null;
644-
ArrayList&lt;Integer&gt; count;
648+
Scanner data = null;
649+
ArrayList&lt;Integer&gt; count; // create an ArrayList to hold counts of numbers, of type Integer
645650
Integer idx;
651+
652+
// Try to open the data file and handle any potential IOExceptions
646653
try {
647654
data = new Scanner(new File("test.dat"));
648655
}
649656
catch ( IOException e) {
650657
System.out.println("Unable to open data file");
651-
e.printStackTrace();
658+
e.printStackTrace(); // print the stack trace for debugging
652659
System.exit(0);
653660
}
654-
count = new ArrayList&lt;Integer&gt;(10);
661+
count = new ArrayList&lt;Integer&gt;(10); // create an ArrayList with an initial capacity of 10
655662
for (Integer i = 0; i &lt; 10; i++) {
656-
count.add(i,0);
663+
count.add(i,0); // initialize the first 10 positions in the ArrayList to hold the value 0
657664
}
658665
while(data.hasNextInt()) {
659-
idx = data.nextInt();
666+
// read each integer from the file and update the count
667+
idx = data.nextInt();
660668
count.set(idx,count.get(idx)+1);
661669
}
662670
idx = 0;
663671
for(Integer i : count) {
672+
// iterate over each element in the ArrayList and print the histogram
664673
System.out.println(idx + " occurred " + i + " times.");
665674
idx++;
666675
}

0 commit comments

Comments
 (0)