Computer Science 2311
Fall 2009
Lab Assignment 4

Contracts

For this program and all later ones, every method is required to have a clear and concise contract. A contract is a comment that describes what a method does. Use the following guidelines for writing contracts.

  1. A contract describes what a method does, not how the method works.

  2. Use complete sentences. A contract should be readable.

  3. Please write a contract in front of the method definition. Normally, you read the contract before you read the method definition.

  4. A contract is not merely a hint. A contract must provide enough detail to explain what a method does. Someone who wants to use a method should be able to read only the contract and the method heading, and know enough to be able to use the method, without looking at the body.

  5. A contract is not a road map to the method body. Do not refer to lines in the body. The idea is that the contract makes sense even to someone who cannot see the method body.

  6. A contract should mention every parameter by name, and explain its significance. Someone reading the contract should understand what to pass as the parameters.

The program

Write a program that reads a word and the name of a file and tells you whether the file contains that word (possibly as part of a longer word). For example, a session with the program might go as follows. Parts in black are written by the program and parts in blue are typed by the user.

  What word should I search for? fox
  Which file shall I look in? animals.txt
  File animals.txt does contain fox.

How to write the program

Write the following static methods, each with a clear and concise contract. Be sure not to try to tell how the method works in the contract.

  1. Write a method that takes two strings, word and text, and returns true if word occurs in text, and false if not. Use the indexOf method of the String class to do this. If A and B are strings, then A.indexOf(B) returns the index where B occurs inside A, or returns -1 if B does not occur in A.

  2. Write a method that takes a string word and a string filename as parameters. It should return 1 if the file named filename contains word, and 0 if not. Its heading should be

      public static int wordOccursInFile(String word, String filename) 
        throws FileNotFoundException
    

    Use a Scanner object to read one line at a time from the file. Line

      Scanner inputStream = new Scanner(new File(filename));
    
    creates object inputStream, which is tied to the file whose name is given by string filename. Now, to read one line, use
      String line = inputStream.nextLine();
    
    You can check whether there is another line using test
      if(inputStream.hasNextLine())
    
    Read each line until there are no more lines. Check whether the word occurs in each line. As soon as you find the word you can stop.

    When you are done reading from the file, do

      inputStream.close();
    

  3. Write a main program that reads a word and a file name from the user and tells whether the file contains the word. You can use a Scanner object to read the strings.

      Scanner keyboard = new Scanner(System.in);
    
    creates an object, keyboard, that reads from the keyboard. Then
      String word = keyboard.nextLine();
    
    gets a line from the keyboard and calls it word. (The newline character at the end of the line is not part of the word.)

Handling errors

What if the file that your program is asked to read does not exist? As your program is written so far, you get an error that is reported in a very unpleasant way. Try your program with a nonexistent file to see what happens.

Modify the program so that, if the file does not exist, it just writes a line saying that the file does not exist.

You can detect errors in Java programs using a try construct. After try you write a fragment, enclosed in braces, that might make an error (or several errors) when it runs. After that, you write catch phrases telling what to do when an error occurs. The following will open a file, but will do a specific thing if the file does not exist.

  try
  {
    Scanner inputStream = new Scanner(new File(filename));
    ...(do something with inputStream)
  }
  catch(FileNotFoundException e)
  {
    ... (do what you should do if the file does not exist)
  }
Modify your definition of method wordOccursInFile so that, if the file does not exist, it returns -1. Remove phrase throws FileNotFoundException from the heading, since that error is now handled by the method.

When you change what a method does, be sure to update the contract too. Keep documentation up to date.

Now modify the main method so that it says the file does not exist when it does not exist. Just check the answer that wordOccursInFile produces.

Turn in a printed copy of your final version. Be sure that your name is in your file, as a comment at the top.