CIS 211 - Computer Science II
Winter, 2003 - A. Hornof

Project 2

Version 2 *

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. 

simModel_2_1.java: Load and save the data file, checking for I/O problems.

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 Data File

The input data file will be formatted exactly as described in Project 1.

The Program Structure

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.

public static String checkReadFile (String file, String [] args)
The method will check if a filename was provided by the user. It will make sure that either the filename provided by the user or the default filename exists and can be read. If not, it will display the following:
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: _
The program will repeatedly try again with the filename typed in by the user, looping until the file can be used or the user quits.

loadModel (String inFileName, ArrayList stringList)

The method will read the contents of the file and call the method addLine() with each line in the file. Be sure to close the file when you are done with it. If there is trouble reading the file, display exactly the following message:
There is trouble reading the simulation data file "<filename>".
The program is quitting.

private static void addLine(String line, ArrayList stringList)

Put each String in order into stringList.

public static String checkWriteFile (String fileName)

Prompt the user with exactly the following:
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: _
If the specified file exists, re-display the message above. If it does not exist, save and quit. If a file cannot be written, prompt the user as follows:
The file "<filename>" cannot be written.
Enter a new filename or 'Q' to quit without saving: _
Whenever the "Q" option is selected, display the following message and quit.
The simulation data file was not saved.

public static void saveModel (String outFileName, ArrayList stringList)

Output each line in stringList to the data file. Be sure to close the file when you are done. If there is trouble writing the file, display the following message:
There was trouble writing the simulation data file "<filename>".
The simulation data file may not have been saved correctly.

Additional Notes

  • Use try and catch blocks to check for I/O errors.
  • Use L&L's Keyboard class for text entry.
  • Whenever checking for a key entered by the user, check for both lowercase and uppercase. Note that your program will not be able to use a data file named "Q" or "q".
  • Use the exact output messages above. We will check for correct output character by character. Cutting and pasting the messages from the web page would be advised. Do not display the underscore; it represents the position of the prompt.

 

simModel_2_2.java: Check for errors in the data file.

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.

The Program Structure

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.

public static boolean checkFileFormat(ArrayList stringList, String fileName)
The method will check if the file is correctly formatted as specified in the Project 1 handout. The method will look for a problematic line in the file. When the first problematic line encountered, the method will report the problem to the user and then immediately return "false". The method will return true if and only if there are no formatting problems in the file.

If there are any formatting problems in the first line, return exactly the following error message:
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.  

simModel_2_3.java

There will be no simModel_2_3.java as suggested earlier.

Java Version

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.)

Grading Criteria

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.