CSCI 3310
Fall 2007
Small Programming Assignment 2

Assigned: September 4
Due: September 14, 11:59pm


Words

For this assignment, a word in a document is defined to be any sequence of letters that is surrounded by nonletters (or the beginning or end of the document). For example, in the document,

T'was brillig, and the slithy toves
did gyre and gimbol in the wabe;
all mimsy were the borogoves,
and the mome raths outgrabe.
"brillig" is a word, but "brill" is not. Notice that, by our definition, "T" is a word, but "T'was" is not, since an apostrophe is not a letter.


The assignment

Write a program that takes two arguments from the command line, first a word then a file name. (That is, the array-of-strings parameter args to main will have args[0] = the word and args[1] = the file name.) The program should report the number of occurrences of the given word in the given file, counting only words as defined above. Case of letters should be ignored. So, for example, if the program is asked to count occurrences of "frost", then it should count "Frost" and "FROST". It should not count "frosty".


Design requirements

It is important that you follow the design requirements. Programs that ignore the requirements will fare poorly in grading.

Design this program in the following form. Create a class of objects that count words in files. The constructor should take a word (a string) and an open file, and the object should remember those things in instance variables. The class should provide the following methods.

  1. convertToLowerCase(A) (a private class method). This class method should take a string A, convert all of its letters to lower case, and return the new string. (You can use the String class toLowerCase method in this function.)

  2. readword() (a public instance method). This function reads information from the file. It should skip over leading nonletter characters, then read as many letters as possible, until it sees a nonletter or the end of the file. It should return the word that it read. If there are no letters left, it should return an empty string.

  3. countWords() (a public instance method). This should successively read words (using readword), checking each to see whether it is the word that is sought, until there are no more words in the file. It should return the number of times the word occurs.

The main program should get the command line arguments, open the file and check that the file-open operation was successful. Then it should create an object that counts words, and ask it how many occurrences there are of the given word in the open file. It should report the result and close the file.

Write the program in two files: WordCounter.java, containing the word-counting class, and Count.java, containing a class holding the main program.


Hints

One way to read a file is to create a new FileReader object. For example, here is an example that reads a file called stuff.txt, and says each character that it read. This is only to show you methods and constructors that you can use.

    File f = new File("stuff.txt");
    if(f.canRead()) {
      try {
        FileReader reader = new FileReader("stuff.txt");
        int ci = reader.read();
        while(ci >= 0) {
	  System.out.println("Got char " + ((char) ci));
          ci = reader.read();
        }
        reader.close();
      }
      catch (Exception E)
      {
	 System.out.println("Hey!  Why can't I read this file?");
      }
Notice that the read method returns an integer, not a character. It returns the character code of a character, or -1 if the end of file has been encountered.

Your object needs to remember an open file. So that will need to be an instance variable, created when the object is constructed. Do not just store the name of the file in the object.

Be sure that instance variables are sensible. For example, if a method needs a local variable (such as ci in the example loop), make that a local variable of the method, not an instance variable in the object.


Submitting your work

Log into one of the Unix machines. Change to the directory that contains your assignment. Then give command

  ~karl/3310/bin/submit S2 WordCounter.java Count.java

You can log into the Unix machines remotely using ssh. You need to have the Ssh Secure Shell client. Log into host login.cs.ecu.edu, or any of the hosts in the lab.