Computer Science 2311
Fall 2007
Laboratory Assignment 11

Handed out: 11/26/07

Read this entire assignment before beginning to work on it.


The assignment

This assignment is similar to assignment 4, part 1. Write a Java program that reads two strings from the user, a word and a file name. It should report the line numbers in the named file where the given word occurs. Number the lines starting at 1.

For example, if the word is ov and the file contains

T'was brillig, and the slithy toves
Did gyre and gimbol in the wabe.
All mimsy were the borogoves,
And the mome raths outgrabe.
then the program should print
1
3
indicating that lines 1 and 3 contain string ov.


How to solve the problem

The Java library has methods that test whether one string is a substring of another. But for this exercise, do not use the library methods. (So do not use any of the find.. methods in the Scanner class, or the indexOf method in the string class, or closely related functions that make the job trivial.) Instead, solve the problem yourself by doing comparisons between characters. Get the n-th character of string s using s.charAt(n).

A class to help out

Write a class SubstringTester with the methods shown below. Copy these headings and contracts into your program.

Objects of your class should have a variable called Pattern of type String. It is the pattern that your object works with.

  //============================================================
  //                         SubstringTester
  //============================================================
  // This initializes an object to hold a particular pattern P.
  //============================================================

  public SubstringTester(String P)

  //============================================================
  //                         occurs
  //============================================================
  // occurs(T) returns true if string Pattern occurs as a substring
  // of string T.  (Pattern is the variable in this object.)
  //
  // For example, if Pattern is "luck" and T is 
  // "Frederick is a lucky man" then occurs(T) returns true.
  //============================================================

  public boolean occurs(String T)

  //============================================================
  //                         occursAt
  //============================================================
  // occursAt(k) returns true if string Pattern occurs
  // as a substring of string T at index k.
  //
  // For example, if Patter is "luck" and T is 
  // "Frederick is a lucky man"
  // then occursAt(T, 15) returns true, since "luck" occurs at index
  // 15 of T, but occursAt(T,10) returns false.  
  // (Blanks count as characters.)
  //============================================================

  private boolean occursAt(String T, int k)

The main method

Create a main method that gets the pattern and file name from the user, opens the file, creates a SubstringTester object, and reads lines from the file, testing whether each contains the pattern. When main is done, it should close the file.

Reading a file

Create a FileReader object and a Scanner object as in assignment 8. Use scan.nextLine() to return the next line of the input, using a scanner called scan. You can use scan.hasNext() to ask whether there is anything left to get. It returns true if there are more things in the input.