CSC 533: Organization of Programming Languages
Fall 2000

HW5: Java Programming


For this assignment, you are to write a Java program that profiles a text file. Your program should read the name of the text file to be processed, then display the total number of words in that file, the number of unique words in the file, and a listing of all the words that appeared exactly once in the file. For example, suppose the file foo.txt contained the following text:

foo bar biz baz boo bar boo baz bingo bango bongo bingo

Your program should produce the following:

Enter the file name: foo.txt Total number of words in the file = 12 Number of unique words in the file = 8 The following words appeared exactly once: foo biz bango bongo


HINT 1: The first thing that you will need to be able to do is read strings from the console and also from a file. The EasyReader class is designed to make these tasks simple. You can create an EasyReader object and initialize it to standard input or a file. Then, there are methods defined for reading values of various types, such as stringInput and intInput. The method isEOF returns true if the end of file has been reached. For example,

EasyReader stdin = new EasyReader(System.in); // initialized to standard input int x = stdin.intInput(); // reads an int value (delineated // by whitespace) and stores in x EasyReader infile = new EasyReader("foo.txt"); // initialized to file named foo.txt while (!infile.isEOF()) { // while not at end of file String word = infile.stringInput(); // read a string (delineated by System.out.println(word); // whitespace) and print to screen }


HINT 2: In order to store the words as they are read in from the file, you should use an extended version of the LinkedList class discussed in lecture (which depends upon the ObjectNode class). As is, this class has methods for inserting an object into the front of the list and displaying the entire list. You will need to add the following methods:

public boolean Contains(Object obj) // postcondition: returns true if obj is stored in the list, else false public boolean Remove(Object obj) // postcondition: if obj is contained in the list, it is removed and returns true // otherwise, the list is unchanged and returns false

Note that when you compare objects in Java, you should NOT use '=='. The '==' operator simply does a bit-by-bit comparison, which in the case of reference types compares addresses as opposed to the underlying data. Instead, you should use the equals method that is defined for objects. For example,

String str1 = "foo", str2 = "foo"; if (str1 == str2) ... // may not return true (if stored at diff. addresses) if (str1.equals(str2)) ... // will return true since their values are the same


HINT 3: Using two LinkedLists, you can complete this assignment as follows: