Computer Science 3200
Section 001
Fall 2015
Programming Assignment 3

Assigned: Monday, September 28
Due: Wednesday, October 7, 11:59pm

Table of contents

  1. Functional requirements
  2. Example
  3. Nonfunctional requirements
  4. Submitting your work


Functional requirements

Write a program that takes a string pattern followed by zero or more file names on the command line. It must search each of the files for lines that contain pattern as a substring.

For each file that has at least one such line, your program must show a line containing the name of the file followed by a colon. Following that should be all of the lines of that file that contain the pattern, each preceded by its line number. (The first line is line 1.)

The pattern does not need to match an entire word. It just needs to be a substring of the line.

If a file has no lines that contain the pattern, then nothing at all should be shown for that file, not even the file name.

If a file cannot be opened, it should be skipped.

This program does not interact with the user. Do not ask the user for file names or for the pattern. Get those from the command line.

Call your program search.java.


Example

After command

java search student orange.txt red.txt yellow.txt
the output might look as follows.
red.txt:
3. all students whose major is
8. and those students are only

yellow.txt:
4. A student has spoken to me

Nonfunctional requirements

Break this program down into a few static methods as follows.

  1. Static method matches(pattern, line) returns true if string pattern occurs in string line, false if not. You are allowed to use methods of the String class, so this method can be very short.

  2. Static procedure showFile(pattern, filename) takes the pattern and the name of a file. It creates a Scanner that reads file filename, shows the output for the file, then closes the Scanner. To test whether pattern occurs in a given line, this function must use your matches method.

  3. The main method should check that there is at least one string in the args array. If so, then it should run showFile for each file from args[1] to args[n−1], if n = args.length.

    If args.length is 0, the main method should say that it needs a pattern and stop.

Your program is required to follow the coding standards, including the following.

  1. Every function is required to have a clear, concise and precise contract.

  2. A function body must not change the value of a parameter.

  3. Make sure that each function does its whole job., not just part of it job.

  4. Do not use static variables.

  5. Every body of an if-statement, loop, etc. must be a compound statement.

  6. If code is only performed at the end of the last iteration of a loop, then it should be written after the loop, not inside the loop.


Submitting your work

You must submit your program using the following method. Email submissions will not be accepted. An excuse that you do not know how to use Linux will not be accepted.

To turn in your work, log into the Linux system, change your directory for the one for assignment 3, and use the following command.

  ~abrahamsonk/3200/bin/submit 3 search.java
After submitting, you should receive confirmation that the submission was successful. If you do not receive confirmation, assume that the submission did not work.

Command

  ~abrahamsonk/3200/bin/submit 3
will show you what you have submitted for assignment 3.

You can do repeated submissions. New submissions will replace old ones.

Late submissions

Late submissions will be accepted for one day after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.