import java.util.ArrayList; import java.util.Scanner; import java.io.File; public class WordFreq { private ArrayList words; private ArrayList counts; public WordFreq() { this.words = new ArrayList(); this.counts = new ArrayList(); } public WordFreq(String fileName) throws java.io.FileNotFoundException { this.words = new ArrayList(); this.counts = new ArrayList(); Scanner infile = new Scanner(new File(fileName)); while (infile.hasNext()) { String nextWord = infile.next(); this.addWord(nextWord); } } public void addWord(String newWord) { int index = words.indexOf(newWord); if (index == -1) { this.words.add(newWord); this.counts.add(1); } else { this.counts.set(index, this.counts.get(index)+1); } } public int wordCount(String desiredWord) { int index = this.words.indexOf(desiredWord); if (index == -1) { return 0; } else { return this.counts.get(index); } } public int numWords() { return this.words.size(); } public void showAll() { for (int i = 0; i < this.words.size(); i++) { System.out.println(this.words.get(i) + ": " + this.counts.get(i)); } } }