School of Engineering and Applied Science
Department of Computer Science
CSci 53 -- Introduction to Software Development
http://www.seas.gwu.edu/~csci53/fall04
Prof. Michael B. Feldman
mfeldman@gwu.edu

Project 5
Due Date: beginning of lecture, Tuesday, Oct. 26, 2004

The goal of this project is to give you more practice with nested loops and other control structures, and to introduce external text files. You have two weeks for this project, but it is more demanding than the earlier ones. Your compilation errors will probably be easy to fix, but your execution results may well be incorrect. If you don't start on it right away, you won't have time to debug the execution errors. Think carefully about your algorithm!

Project:

Assume the definition of a calendar date we used in Project 4.

Develop a program that will ask the user for an arbitrary starting date and an arbitrary ending date, then output, to a file named "test.dat" a line for each day from the starting date to the ending date, inclusive. The starting and ending dates do not have to be in the same year.

For example, if the user enters a starting date of May 10, 1999 and an ending date of September 15, 2002, the file will contain a line for each day from May 10 through December of 1999, all of 2000, all of 2001, and all dates in 2002 through September 15. Remember, when turning in your results,be sure to include these output files in their complete and unedited form. This may mean that there will be several pages to turn in.

Your dates should be output in the following format: May 3, 2002When your program completes its run, test.datwill be an ordinary text file in your directory, which you can read with vi, type out (cat) to the screen, print, download, or whatever.

In this project you are NOT required to validate the dates or to test with invalid ones; you can assume the user will always enter valid data. However, your test plan must include several date ranges that will test whether your program "rolls over" properly from month to month and year to year. In Project 6 we'll combine this project with the Project 4 validation ideas.

Notes about Files

It is important, even in an introductory course, to learn something about external files, because almost all "real" programs use them. Unfortunately, the textbook discussion of files appears rather late in the book, in Section 8.3 (p. 471-473). On the other hand, this discussion is straightforward and you can understand it with just your knowledge so far. Let's look at the example from that section, programs53/TestData.java.

    1. //**************************************************************
   2. //  TestData.java       Author: Lewis/Loftus
   3. //
   4. //  Demonstrates the use of a character file output stream.
   5. //*************************************************************
   6.
   7. import java.util.Random;
   8. import java.io.*;
   9.
  10. public class TestData
  11. {
  12.    //------------------------------------------------------------
  13.    //  Creates a file of test data that consists of ten lines each
  14.    //  containing ten integer values in the range 10 to 99.
  15.    //------------------------------------------------------------
  16.    public static void main (String[] args) throws IOException
  17.    {
  18.       final int MAX = 10;
  19.
  20.       int value;
  21.       String file = "test.dat";
  22.
  23.       Random rand = new Random();
  24.
  25.       FileWriter fw = new FileWriter (file);
  26.       BufferedWriter bw = new BufferedWriter (fw);
  27.       PrintWriter outFile = new PrintWriter (bw);
  28.
  29.       for (int line=1; line <= MAX; line++)
  30.       {
  31.          for (int num=1; num <= MAX; num++)
  32.          {
  33.             value = rand.nextInt (90) + 10;
  34.             outFile.print (value + "   ");
  35.          }
  36.          outFile.println ();
  37.       }
  38.
  39.       outFile.close();
  40.       System.out.println ("Output file has been created: " + file);
  41.    }
  42. }

Here are the key things to notice and incorporate in your program:

  1. a new import statement (line 8) (your project needs java.io but doesn't need Random);
  2. the phrase throws IOException in line 16;
  3. lines 21, 25, 26, and 27, which set up the file;
  4. lines 34 and 36, which show how to write to this file;
  5. line 39, which closes the file and is important to do just before your program ends.
Before writing your own program, make sure you copy, compile, and run this one so you can see how the file is created.

What to submit

You must follow the process given in Systematic Software Development and the sample project packet distributed in class.

Your grade will be calculated on a 20-point basis, as follows:

Extra credit:

We'll continue the 2-point bonus for getting an early start. If you e-mail your "framework" listing file to Prof. Feldman, and the time stamp on the e-mail is no later than 5 PM, Tuesday, Oct. 19, 2004, you will be awarded 2 extra project points. The "framework" must be a listing (.txt) file, with no compilation errors or warnings, that contains the declared variables, and a set of comments inserted for the main algorithm steps.

MBF 10/11/04