| 
cs205: engineering software? | 
(none) 20 September 2010  | 
cs205 Friday 1 September 2006
| Assignments Due | 
How can we increase our confidence that a program works as intended?
For what kinds of programs is exhaustive testing possible?
What is a successful test case?
What is the difference between testing and analysis?
What are the advantages and disadvantages of spending time on analysis instead of testing?
package wordcount;
import java.io.*;
import java.util.Scanner;
public class WordCount {
   public static void main(String[] args) {
      int linecount = 0;
      int wordcount = 0;
      int lencount = 0;
      if (args.length < 1) {
         System.err.println("Usage: first parameter must name a file.");
         System.exit(0);
      }
      // Java compiler complains about non-initialization without this.
      Scanner s = null; 
      try {
         s = new Scanner(new File(args[0]));
      } catch (FileNotFoundException e) {
         System.err.println("Unable to open file: " + e);
         System.exit(0);
      }
      while (s.hasNextLine()) {
         String line = s.nextLine();
         linecount++;
         Scanner linescanner = new Scanner (line);
         while (linescanner.hasNext()) {
            String word = linescanner.next();
            wordcount++;
            lencount += word.length();
         }
      }
    
      System.out.println ("Lines: " + linecount + " Words: " + wordcount 
         + " Characters in words: " + lencount);
   }
}
Edsger Dijkstra said, Testing can establish the presence of errors, but never their
abscence.  Is this always true?