Project 2 - Addendum

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

 

Modifications and advice for Project 2 are listed here in reverse chronological order, with new additions at the top.

1/22/03

Q: Should the output file be one String per line as in Project 1?

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.

 

1/21/03

Q: In simModel_2_2.java, should the output be EXACTLY

The first line of the file "world.txt" should be

and so on?

A: Not always. Only if world.txt is the actual input filename. Otherwise, world.txt should be replaced with the actually filename being used.

 

Q: The top of the Project 2 assignment suggests that we should be creating our own exceptions. I am curious if we are indeed supposed to be doing this, that is, creating our own exceptions and throwing them if the format if the file is incorrect.

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.

 

1/20/03

Q: How can I check if a String can be read 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.

 

Q: What would a couple correct outputs for simModel_2_2 look like?

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

 

You may optionally create a CheckInts_1toN() method.

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.

 

1/19/03

Q: How do I check if a file exists?

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.

 

Q: What would one sample correct user interaction with simModel_2_1 look like?

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

 

How to access the Keyboard class - 1/17/03

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:

  1. Get the Keyboard.java file from the CD that came with the L&L textbook, 3rd edition, or from the Keyboard Class web page.
  2. Put the file into your project directory, the same directory with simModel_2_1.java and simModel_2_2.java.
  3. Edit Keyboard.java by commenting out this line at the top of the file:
    // package cs1;
  4. (This step is not necessary -A.Hornof 1/21/03.) Add the following line to the top of your program code:
    import Keyboard;
  5. You should now be able to access the Keyboard class.

If you know how to set up a classpath, follow these steps:

  1. Get the file cs1.jar file from the CD that came with the L&L textbook, 3rd edition, or from the Keyboard Class web page.
  2. Put the file into your classpath directory.
  3. Add the following line to the top of your program code:
    import cs1.Keyboard;
  4. You should now be able to access the Keyboard class.

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.

 

Don't worry about quitting while in loadModel()

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

 

Q: How do I prevent the calling of loadModel() if the user typed a "Q" inside of checkReadFile()?

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.

 

Q: What exactly is stringList?

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.