Modifications and advice for Project 2 are listed here in reverse chronological order, with new additions at the top.
A: No, the output file should end up pretty much just like the input file, with one entry per line. This way, the program can keep reading and saving the same data file over and over.
The first line of the file "world.txt" should be
A: Not always. Only if world.txt is the actual input filename. Otherwise, world.txt should be replaced with the actually filename being used.
A: Good point. The answer is no, please just catch the various IO exceptions, and the exception that is used to check if a number can be parsed as an Integer.
A: Look at how L&L do it in Keyboard.java, but you should probably only check for the one relevant exception. Also, look at the methods associated with the Integer class.
A: Everything from simModel_2_1, plus outputs such as the following:
> java simModel_2_2 empty_file.txt Maritime simulation data file error: The first line of the file "empty_file.txt" should be "Maritime Simulation World State". > java simModel_2_2 long-world.txt The simulation data file "long-world.txt" cannot be read. Enter a new file name or 'Q' to quit: world-long.txt Maritime simulation data file error: The line of the file "world-long.txt" "Island North_America 42" 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. >
This method would have exactly the following signature
public static boolean CheckInts_1toN(String string1, String string2, int n)
and would take two strings and return "true" if and only if both can be parsed as integers between 1 and n, inclusive. You do not need such a method, but you may create one if you like. Do not create any other new methods.
A: The "File" class has some methods that would be helpful for this. See the Java API for help with using the File class. Note that there is a difference between creating a File object in your program and creating a file on the disk. You can create a File object, for example, for a file that doesn't even exist, and then check to see if it actually exists using a method in the File class.
A: As follows. Note that the world.txt file is saved as read-only in the file system.
> java simModel_2_1 duh.txt The simulation data file "duh.txt" cannot be read. Enter a new file name or 'Q' to quit: world.txt Overwrite existing data file "world.txt"? (Y/N) y The file "world.txt" cannot be written. Enter a new filename or 'Q' to quit without saving: new-world.txt Overwrite existing data file "new-world.txt"? (Y/N) n Enter a new filename or 'Q' to quit without saving: new-world-2.txt > java simModel_2_1 Overwrite existing data file "world.txt"? (Y/N) n Enter a new filename or 'Q' to quit without saving: new-world-2.txt Overwrite existing data file "new-world-2.txt"? (Y/N) n Enter a new filename or 'Q' to quit without saving: q The simulation data file was not saved. > _
There are two ways that you can give your program access to the Keyboard class. Either one will work.
If you don't know how to set up a classpath, follow these steps:
If you know how to set up a classpath, follow these steps:
When we grade, we will put cs1.jar in our classpath, and also put a copy of Keyboard.class into your e-turnin directory, so either method will work provided that you do not modify the Keyboard class.
The Project 2 handout instructs you to display the following message if there is trouble reading the file:
There is trouble reading the simulation data file "<filename>". The program is quitting.
This is very unlikely to happen and it would be difficult to create a test case that would make it happen. So just catch the IOException and display it with System.out.println().
A: The first thing that loadModel() should do is make sure that inFileName is not a "Q". If it is, the rest of the method should not be executed. The methods checkWriteFile() and saveModel() should do the same thing. It is simple and it works.
A: The stingList is an ArrayList in which each element corresponds to one line in the data file. The first element of the ArrayList is the first line of the data file, the second element is the second line in the data file, etc.