ENGR 131: Stepwise Refinement / Iterated Development

M.S. Branicky (Copyright 2006, 2007)

Iterated development or successive refinement is a type of program development where you write or modify a minimal/similar program and always proceed by making small changes to obtain other working programs.

This is common practice among top programmers for building complex applications. In fact, it was used by Paul Buchheit — a retired-by-30 Case grad — to create Gmail and heavily advocated in his talk at Case in October 2006. It was also mentioned in the October 2007 talk by Rick Saada (son of Case Professor Tony Saada) of Flying Labs Software as important in the development of their latest massively multiplayer on-line role-playing game, Pirates of the Burning Sea.

You might be a few years away from being a Google billionaire or computer game tycoon. However, below are two simple examples of stepwise refinement, with "Task Breakdowns" provided to help you learn this style of development. For more on this topic, you should read the document entitled Program Development Plan linked here and on the course webpage.

Problem 1

/*  Stepwise Refinement / Iterated Development Example
    -- M. Branicky, 10/30/06, 10/10/07

GOAL: Create two files, odds.txt and evens.txt, with the
first one containing the odd numbers from 1 to 1000,
and the second one containing the even ones.

HINT: Start with the file WriteFile.java (reproduced below)

TASK BREAKDOWN:
---------------
STEP 0: Copy WriteFile.java from the ENGR 131 Code Repository
        Copy WriteFile.java to a new program OddsEvens.java
STEP 1: Edit OddsEvens.java to save 1-1000 in the file all.txt
        Compile, Run, Debug.  Look at output.
STEP 2: Edit OddsEvens.java to save the odds to file odds.txt
        Compile, Run, Debug.  Look at output.
STEP 3: Edit OddsEvens.java to also save evens to file evens.txt
        Compile, Run, Debug.  Look at output.  We're done!
*/

// This file demonstrates writing data to a text file using
// the PrintWriter class and the File class
//
//  -- M. Branicky, 10/09/06, 10/07/07

import java.io.PrintWriter; // needed for output processing using PrintWriter
import java.io.File;        // needed for file input/ouput

public class WriteFile {
  public static void main(String args[]) throws Exception {  // NOTE change here
    // Open the file
    File outfile = new File("out.txt");

    // Create a PrintWriter for file output
    PrintWriter output = new PrintWriter( outfile );

    // Write output to the file
    output.println("Prints an entire line, plus a carriage return.");
    output.print("Prints a string without ... ");
    output.print("a new line ... ");
    output.println("."); // end the line
    output.println();
    output.println("The square root of 2 is " + Math.sqrt(2.0));
    output.printf("The square root of 2 is %f%n", Math.sqrt(2.0));
    output.printf("%d + %d = %d%n", 2, 2, 2+2);
    output.println(2 + " + " + 2 + " = " + (2+2));
    output.println();

    // Close the file
    output.close();
  }
}

Problem 2

/*  Stepwise Refinement / Iterated Development Example -- M. Branicky, 10/30/06, 10/10/07

BACKGROUND: The file userN.dat (produced by GLRatings.java) contains a tab-separated list of integers:
"user_id | userN", where userN represents the number of movies that user user_id has reviewed.

GOAL: Read in userN.dat and output (to the screen) a list of the data for those
users who have reviewed more than 200 movies (so-called "heavy users")

HINT: Start with the file ReadFile.java (reproduced below)

TASK BREAKDOWN:
---------------
STEP 0: Copy ReadFile.java from the ENGR 131 Code Repository
        Copy ReadFile.java to a new program HeavyUsers.java
STEP 1: Edit HeavyUsers.java to display all the data.
        Compile, Run, Debug.  Look at output.
STEP 2: Edit HeavyUsers.java to display only the heavy users' data
        Compile, Run, Debug.  Look at output.
*/

// This file demonstrates reading data from a text file using
// the Scanner class and the File class
//  -- M. Branicky, 10/09/06, 10/07/07

import java.util.Scanner; // needed for input processing using Scanner
import java.io.File;      // needed for file input/output

public class ReadFile {
  public static void main(String args[]) throws Exception {  // NOTE change here
    // Open the file and create the Scanner
    File infile = new File("MadlibData.txt");
    Scanner input = new Scanner( infile );

    // Variables to be used in the story
    double number = 0.0;
    String pluralNoun = "", adjective = "";

    // Get input from file
    while (input.hasNext()) {
      pluralNoun = input.next();
      adjective = input.next();
      number = input.nextDouble();

      // Display story
      System.out.printf("After the man ate %f %s, he felt very %s.\n",
         number, pluralNoun, adjective);
    }

    // Close the file
    input.close();
  }
}

Created: 2007-10-10. Last Modified: 2007-10-10. © Michael S. Branicky