//An instance of this class records information on the occurences of a word in a text file: // 1) the word, 2) number of occurences, 3) line-numbers in the file where the word occurs // //PUBLIC INTERFACE // Constructor // WordRecord(String word, int lineNumber) // @param word: a word obtained from the text file // @param lineNumber: the line-number of the first occurence of the word // Accessors // String getWord() // @return: the word recorded in this WordRecord // int getFrequency() // @return: the frequency of occurence of the word in this WordRecord // int[] getLineNumbers() // @return: a list of line-numbers of the occurences of the word in this WordRecord // Mutator // void update(int lineNumber) // @param lineNumber: a line-number of the file where the word occurs // Postcondition: the frequency of occurence the word has been incremented // Postcondition: lineNumber is stored exactly once in the list of line-numbers // Postcondition: the list of line-numbers is in ascending order // Other Methods // String toString() // @return: A printable immage of this WordRecord with line-numbers in ascending order // Overrides the inherited toString() method // int compareTo(WordRecord other) // @return: a negative, zero or positive integer as the word of this WordRecord is // lexicographically less than, equal to or greater than the word of other // Implements the Comparable Interface // import java.util.*; public class WordRecord implements Comparable { //Instance (State) Variables private String word; //The word private int frequency; //The number of occurences of word in the text file private ArrayList lineNumbers; //Line-numbers of occurences of word in the file //Constructor public WordRecord(String word, int lineNumber) { this.word = word; this.frequency = 1; this.lineNumbers = new ArrayList<>(); this.lineNumbers.add(lineNumber); } //Accessor // @return: the word recorded in this WordRecord public String getWord() { return this.word; } //Accessor // @return: the frequency of occurence of the word in this WordRecord public int getFrequency() { return this.frequency; } //Accessor // @return: a list of line-numbers of the occurences of the word in this WordRecord public int[] getLineNumbers() { int[] numbers = new int[this.lineNumbers.size()]; for (int k = 0; k < numbers.length; k++) numbers[k] = this.lineNumbers.get(k); return numbers; } //Mutator // @param lineNumber: a line-number of the file where the word occurs // Postcondition: the frequency of occurence the word has been incremented // Postcondition: lineNumber is stored exactly once in the list of line-numbers // Postcondition: the list of line-numbers is in ascending order public void update(int lineNumber) { this.frequency++; int index = this.lineNumbers.size() - 1; while (index >= 0 && this.lineNumbers.get(index) > lineNumber) index--; if (index == -1 || this.lineNumbers.get(index) != lineNumber) this.lineNumbers.add(index + 1, lineNumber); } //Override // @return: A printable immage of this WordRecord with line-numbers in ascending order public String toString() { return pad(this.word, 12) + " Frequency: " + (this.frequency < 10 ? " " : "") + this.frequency + " On Lines " + this.lineNumbers ; } //Helper (toString()) // @return a String with parameter str extended to length given in the parameter private static String pad(String str, int length) { while (str.length() < length) str += " "; return str; } //Implements the compareTo() method specified in the Comparable Interface // @return: a negative, zero or positive integer as the word of this WordRecord is // lexicographically less than, equal to or greater than the word of other public int compareTo(Object other) { WordRecord that = (WordRecord)other; return this.word.compareToIgnoreCase(that.word); } }