CIS 211 - Computer Science II
Winter, 2003 - A. Hornof
This Project is due on Wednesday, January 22, at 5 PM.
* The only difference between Version 1 and 2 is that simModel_2_2.java has been specified, below.
The point of the assignment is for you to learn how to handle problems that occur during the I/O process by using exceptions, how to check for problems by creating your own exceptions, and how to create objects from data read in from a file.
The final program will read the same simulation data file format specified in Project 1, check for I/O and file formatting errors, and provide specified feedback to the user if there are problems. When the file is read in correctly, the program will then create a set of objects, describe their contents on the screen, and rewrite the contents to a new data file.
This program is very similar to Project 1, except that you will check for I/O errors, interact with the user (exactly as specified) when I/O errors occur, and use a single data file that you overwrite after checking with the user.
The input data file will be formatted exactly as described in Project 1.
The main() method should be exactly as follows.
public static void main(String args[])
{
String fileName = "world.txt"; // Default data file.
ArrayList stringList = new ArrayList (); // Contents of file.
fileName = checkReadFile(fileName, args); // Decides file to read.
loadModel(fileName, stringList); // Load the input file.
fileName = checkWriteFile(fileName); // Decides file to write.
saveModel(fileName, stringList); // Store the output file.
}
There will be an additional five methods, all in the simModel_2_1.java class, as follows. Do not create any other additional methods.
The simulation data file "<filename>" cannot be read. Enter a new file name or 'Q' to quit: _
Example:
The simulation data file "world.txt" cannot be read. Enter a new file name or 'Q' to quit: _
loadModel (String inFileName, ArrayList stringList)
There is trouble reading the simulation data file "<filename>". The program is quitting.
private static void addLine(String line, ArrayList stringList)
public static String checkWriteFile (String fileName)
Overwrite existing data file "<filename>"? (Y/N) _
If the user enters "Y", proceed to save, and then quit. Otherwise, prompt the user with:
Enter a new filename or 'Q' to quit without saving: _
The file "<filename>" cannot be written. Enter a new filename or 'Q' to quit without saving: _
The simulation data file was not saved.
public static void saveModel (String outFileName, ArrayList stringList)
There was trouble writing the simulation data file "<filename>". The simulation data file may not have been saved correctly.
After you have a fully debugged simModel_2_1.java, save a backup copy in a safe place so you can turn it in. Then, copy the file to simModel_2_2.java and enhance simModel_2_2.java as follows.
Modify your main() method to match the following. Only the "if" statement and the call to checkFileFormat() are new.
public static void main(String args[])
{
String fileName = "world.txt"; // Default data file.
ArrayList stringList = new ArrayList (); // Contents of file.
fileName = checkReadFile(fileName, args); // Decides file to read.
loadModel(fileName, stringList); // Load the input file.
// If the file is correctly formatted
if (checkFileFormat (stringList, fileName))
{
fileName = checkWriteFile(fileName); // Decides file to write.
saveModel(fileName, stringList); // Store the output file.
}
}
Then, write this one additional method that will check the format of the data file.
Maritime simulation data file error: The first line of the file "world.txt" should be "Maritime Simulation World State".
If there are any formatting problems in the second line, return exactly the following error message:
Maritime simulation data file error: The second line of the file "world.txt" should be "World_size INT-X INT-Y" in which INT-X and INT-Y are integers between 1 and 100.
If there are any formatting problems in any subsequent line, return exactly the following error message:
Maritime simulation data file error: The line of the file "world.txt" "<The line appears here>" should be either "Ship NAME [Cruise_Ship | Pirate | Sailing | Tanker] INT-X INT-Y" or "Island NAME INT-X INT-Y" in which INT-X and INT-Y are integers between 1 and 100.
You will fill in <The line appears here> with the problematic line.
Do not create any additional methods.
Be sure to test your program with all possible file formatting problems, including but not limited to extra words on a line and numbers out of range or not integers. You will need to be creative to think of all possible problems, and you will need to test them all. Perhaps create a series of test files and a batch file or shell script that calls them all in succession.
There will be no simModel_2_3.java as suggested earlier.
Use Java version 1.3.1 or 1.4.0. Your program header should indicate which you used. (Version 1.3.1 is permitted because this is the latest version that runs on Mac OS X.)
You will only receive credit for programs that compile and run. Your program must solve the problem as specified. You will need to make a list of all possible situations that your program could encounter, and thoroughly test all of them.