Justin
2013-04-17 11:40:05 UTC
Assignment:
For this assignment you are to write a program that handles text files. The assignment requires three classes (plus library classes); we've given you two - FreqStudy, the driver, and another class, WordCount.
FreqStudy:
import java.util.*;
import java.io.*;
public class FreqStudy{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println("enter file name");
String fileName = scan.next();
Scanner scan2 = new Scanner(System.in);
System.out.println("enter words to search for");
System.out.println("enter lower case, separated by spaces");
String wordString = scan2.nextLine();
WordFreq f = new WordFreq(fileName,wordString);
f.readLines();
f.reportFrequencies();
}
}
WordCount:
public class WordCount{
private String word;
private int count;
public WordCount(String w){
word = w;
count = 0;
}
public String getWord(){
return word;}
public int getCount(){
return count;}
public void incCount(){count++;}
public String toString() {
return(word + " --- " + count);
}
public boolean equals(Object other){
WordCount i = (WordCount)other;
return (this.word.equals(i.word));
}
}
Your job is to write WordFreq, the heart of the application. It MUST extend the Echo class (from Ch 9 of text):
Echo:
import java.util.Scanner;
import java.io.*;
public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file
public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}
public void readLines(){ // reads lines, hands each to processLine
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}
public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}
As demonstrated in the example run above, an external text file name is entered first. Then any number of individual words are entered, all lower case, and separated by spaces. After doing its analysis, the program then displays a chart that gives the frequencies in the text of the indicated words, to four decimal places. Thus 2.42% of all of the words in the Heart of Darkness text file are "I", 1.33% are "he", and so forth. Pretty clearly Heart of Darkness is narrated in the first person, and is mostly about men.
Further requirements and tips:
* you MUST use an array of WordCount objects to keep track of the occurrences of the indicated words.
* Use printf from Chapter 5 to format your output.
* If s is a String, then this String class method call:
s.split(" "); // this is a single space surrounded by quote marks
returns an array of strings that consists of the tokens (separated by white space) in s. For example, given
String s = "i he his she hers";
String[] words = s.split(" ");
So, here is what I have for code so far for my WordFreq class:
import java.io.IOException;
import java.util.*;
public class WordFreq extends Echo{
private String name;
private String wordString;
private int total;
private String[] words;
public WordFreq(String fileName, String word){
super(fileName);
wordString = word;
}
public void splitLine(){
String s = wordString;
words = s.split(" ");
}
public void countWords(){
StringTokenizer st = new StringTokenizer(name);
while (st.hasMoreTokens()){
for(int j = 0; j < words.length; j++){
total++;
if (st.nextToken() == words[j])
words[j].incCount();
}
}
}
public void reportFrequencies(){
for (int j = 0; j < words.length; j++){
System.out.println(words[j].getWord() + " " + (words[j].getCount()/total));
}
}
}
I am getting errors on line 26 and two on line 33. It cannot find the incCount(), getWord(), and getCount() methods.
Any help would be greatly appreciated!