Skip to content

Commit 27496f7

Browse files
committed
added comments to 3.6 code blocks
1 parent 3ac8b5d commit 27496f7

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -833,15 +833,16 @@ public class HistoArray {
833833

834834
<program xml:id="python-input-file2" interactive="activecode" language="python">
835835
<code>
836-
def main():
836+
def main():
837+
""" Program to read a file and count the frequency of words in the file. """
837838
data = open('alice30.txt')
838-
wordList = data.read().split()
839+
wordList = data.read().split() # split the file into a list of words
839840
count = {}
840-
for w in wordList:
841+
for w in wordList: # iterate over the list of words
841842
w = w.lower()
842843
count[w] = count.get(w,0) + 1
843-
keyList = sorted(count.keys())
844-
for k in keyList:
844+
keyList = sorted(count.keys()) # sort the keys in alphabetical order
845+
for k in keyList: # iterate over the sorted list of keys
845846
print("%-20s occurred %4d times" % (k, count[k]))
846847
main()
847848
</code> <tests> </tests>
@@ -880,36 +881,39 @@ main()
880881

881882
<program xml:id="java-use-input-file" interactive="activecode" language="java" datafile="alice30.txt">
882883
<code>
883-
import java.util.Scanner;
884-
import java.util.ArrayList;
885-
import java.io.File;
886-
import java.io.IOException;
887-
import java.util.TreeMap;
884+
import java.util.Scanner;
885+
import java.util.ArrayList; // import the ArrayList class to use dynamic arrays
886+
import java.io.File;
887+
import java.io.IOException;
888+
import java.util.TreeMap; // import the TreeMap class to use a map for counting word frequencies
889+
/**
890+
* Program to read a file and count the frequency of words in the file.
891+
*/
888892
public class HistoMap {
889893
public static void main(String[] args) {
890894
Scanner data = null;
891-
TreeMap&lt;String,Integer&gt; count;
895+
TreeMap&lt;String,Integer&gt; count; // create a TreeMap to hold word counts, mapping each word (String) to its frequency (Integer)
892896
Integer idx;
893897
String word;
894898
Integer wordCount;
895-
try {
899+
try { // Try to open the data file and handle any potential IOExceptions
896900
data = new Scanner(new File("alice30.txt"));
897901
}
898902
catch ( IOException e) {
899903
System.out.println("Unable to open data file");
900904
e.printStackTrace();
901905
System.exit(0);
902906
}
903-
count = new TreeMap&lt;String,Integer&gt;();
907+
count = new TreeMap&lt;String,Integer&gt;(); // create a TreeMap to hold word counts
904908
while(data.hasNext()) {
905-
word = data.next().toLowerCase();
906-
wordCount = count.get(word);
909+
word = data.next().toLowerCase(); // read each word from the file, convert it to lowercase
910+
wordCount = count.get(word); // get the current count for the word from the TreeMap
907911
if (wordCount == null) {
908912
wordCount = 0;
909913
}
910-
count.put(word,++wordCount);
914+
count.put(word,++wordCount); // increment the count for the word and put it back into the TreeMap
911915
}
912-
for(String i : count.keySet()) {
916+
for(String i : count.keySet()) { // iterate over the keys (words) in the TreeMap
913917
System.out.printf("%-20s occurred %5d times\n", i, count.get(i) );
914918
}
915919
}

0 commit comments

Comments
 (0)