cs205: engineering software?
20 September 2010

CS205 Notes 5 (1 September 2006)

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?


For ps2, you will need to read a file. The Scanner class (provided in the Java API and specified in the ps2 handout) is an easy way to read a file. Here's an example program that uses it to count the number of lines and words in a file. You may find some of this code a useful starting point for the file reading code you need for ps2.
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.");

      // 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);

      while (s.hasNextLine()) {
         String line = s.nextLine();
         Scanner linescanner = new Scanner (line);
         while (linescanner.hasNext()) {
            String word = linescanner.next();
            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?