Project 5
Due Date: beginning of lecture, Thursday, Oct. 24, 2002
The goal of this project is to give you more practice
with nested loops and other control structures, and to introduce external
text files.
Project:
Assume the definition of a calendar date we used in
Project 4:
- the month must be in the range 1-12, where January = 1 and December
= 12
- the day must be in the range 1-31;
- the year must be no earlier than 1753, the first full year of our current
(Gregorian) calendar;
- the month/day/year combination must form a valid date; that
is,
- the day can't be 31 in a 30-day month;
- in February, the day can't be greater than 28 (non-leap year) or
29 (leap year).
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, 2002. When
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 should 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:
- a new import statement (line 8) (your project needs java.io
but doesn't need Random);
- the phrase throws IOException in line 16;
- lines 21, 25, 26, and 27, which set up the file;
- lines 34 and 36, which show how to write to this file;
- 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:
- 6 points -- analysis and design (including algorithm in structured
English)
- 4 points -- test plan
- 6 points -- correct execution of program according to test plan
- 4 points -- layout and style of program source code
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, Monday, Oct. 21, 2002, you will be awarded
2 extra project points. The "framework" must be a listing (.lis)
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/15/02