Loyola College in Maryland

CS 202 - Computer Science II
Fall 2004


Loyola College > Department of Computer Science > Dr. James Glenn > CS 202 > Homework > Solutions for Exam #2 Practice

Problem 1: Write a method called grep that takes three arguments: the name an input file, the name of an output file, and a string to search for within the input file. The method should open the input file, create the output file, and copy to the output file every line from the input file that contains the search string. For example, if the input file contains

Joanna: So where do you work, uh, Peter?
Peter: Initech
Joanna: And, uh, what do you do there?
and the search string was Peter then the output file should contain
Joanna: So where do you work, uh, Peter?
Peter: Initech
public static void grep(String inname, String outname, String search) throws IOException
{
  BufferedReader in = new BufferedReader(new FileReader(inname));
  PrintWriter out = new PrintWriter(new FileWriter(outname));

  String line;
  while ((line = in.readLine()) != null)
    if (line.indexOf(search) != -1)
      out.println(line);

  out.close();
}

Problem 2: Write a method that takes as its parameter the name of a file containing start and stop times in the form "hh:mm hh:mm" and adds up and returns the total time intervals in minutes. For example, if the input file is

3:15 4:00
10:30 10:40
then the method would return 55. You may assume that there is a method computeInterval(int startHr, int startMin, int endHr, int endmin) that computes the number of minutes between two times and that the times will not wrap around days.
public int readTimes(String fname) throws IOException
{
  BufferedReader in = new BufferedReader(new FileReader(fname));

  int total = 0;
  String line;
  while ((line = in.readLine()) != null)
  {
    StringTokenizer tok = new StringTokenizer(line, ": ");
    int h1 = Integer.parseInt(tok.nextToken());
    int m1 = Integer.parseInt(tok.nextToken());
    int h2 = Integer.parseInt(tok.nextToken());
    int m2 = Integer.parseInt(tok.nextToken());

    total += computeInterval(h1, m1, h2, m2);
  }

  return total;
}

Problem 3: Redo problem 2, but do the computation in seconds. Assume that you have an appropriate version of the computeInterval method, and that each time in the file may or may not contain the seconds (assume 0 if the seconds are omitted). For example, the input file may contain

3:15 3:17:20
4:00:01 4:00:10
in which case the new method should return 149.
public int readTimes(String fname) throws IOException
{
  BufferedReader in = new BufferedReader(new FileReader(fname));

  int total = 0;
  String line;
  while ((line = in.readLine()) != null)
  {
    StringTokenizer tok = new StringTokenizer(line);
    String start = tok.nextToken();
    String end = tok.nextToken();
    
    tok = new StringTokenizer(start, ":");
    int h1 = Integer.parseInt(tok.nextToken());
    int m1 = Integer.parseInt(tok.nextToken());
    int s1 = 0;
    if (tok.hasMoreTokens())
      s1 = Integer.parseInt(tok.nextToken());

    tok = new StringTokenizer(end, ":");
    int h2 = Integer.parseInt(tok.nextToken());
    int m2 = Integer.parseInt(tok.nextToken());
    int s2 = 0;
    if (tok.hasMoreTokens())
      s2 = Integer.parseInt(tok.nextToken());

    total += computeInterval(h1, m1, s1, h2, m2, s2);
  }

  return total;
}

Problem 4: Write a method called blankCount that, given the name of a file as an argument, returns the number of blank lines in the file. If the file does not exist, blankCount should return 0. If any other error occurs when reading from the file the method should use System.err.println to display an error message and then call System.exit(0) to terminate the program. blankCount must not throw any exceptions back to the caller.

public int blankCount(String fname)
{
  try {
    BufferedReader in = new BufferedReader(new FileReader(fname));

    int count = 0;

    String line;
    while ((line = in.readLine()) != null)
      if (line.length() == 0)
        count++;

    return count;
  }
  catch (FileNotFoundException e)
  {
  }
  catch (IOException e)
  {
    System.err.println("Error reading file " + fname);
    System.exit(0);
  }

  return 0;
}

Problem 5: Consider the following recursive definition of the factorial function.

public static int factorial(int n)
{
  if (n == 0)
    return 1;

 else if (n < 0)
    throw new IllegalArgumentException();

  else
    return n * fact(n - 1);
}

Problem 6: Consider the following recursive function.

public static int aCount(int n)
{
  if (n < 2)
    return 0;
  else
    return 1 + aCount(n - 1) + aCount(n - 2);
}

Problem 7: Let w0 = 2, w1 = w2 = w3 = 1, and, for k > 3, wk = 2 if wk-1 = wk-2 = wk-3 = 1 and wk = 1 otherwise. Write a recursive method that computes wk.

public int w(int k)
{
  if (w == 0)
    return 2;
  else if (w <= 3)
    return 1;
  else if (w(k - 1) == 1 && w(k - 2) == 1 && w(k - 3) == 1)
    return 2;
  else
    return 1;
}

Problem 8: The following code fragment reads lines from a file and then displays those that are of above average length.

String[] file = new String[100];
ArrayList file = new ArrayList();
int lineCount = 0;
int totalLength = 0;

String line;
while ((line = in.readLine()) != null)
{
  file[lineCount] = line;
  file.add(line);
  totalLength += line.length();
  lineCount++;
}

double average = (double)totalLength / lineCountfile.size();

for (int l = 0; l < lineCountfile.size(); l++)
  if (file[l].length() ((String)(file.get(l))).length() > average)
    System.out.println(file[l]file.get(l));