Additional clarification (added 1/11/03): Use a FileWriter as discussed in Chapter 8 to send your output to the output file.
Project 1 is due in two parts. Part 1, which is specified here, is due on Monday, January 13, at 5 PM. Submit via e-turnin, which appears to require JavaScript. Part two will be specified in a subsequent document and is due Tuesday, January 21.
The point of the assignment is for you to learn (1) the basics of moving data between a disk file and a program, (2) how to handle problems that sometimes occur with this process, and (3) how to compartmentalize the error-handling code by using exceptions.
The project is the first step in building a piece of software that will simulate maritime activity -- ships sailing between islands, and various interactions among the ships. The first step here is just to retrieve, display, and store a data file associated with the simulation. Subsequent projects will continue to build the project.
The program will retrieve data from disk, and display the state of the "world" in a maritime simulation.
When executed, SimModel_1_0.java will take a single argument, which will be the name of a textual data file. If no argument is provided, the program will look for the file "world.txt." The file will be formatted as described here.
The Data File
The first two lines of the data file will be exactly as follows (without the preceding tab character):
Maritime Simulation World State World_size INT-X INT-Y
Subsequent lines will be as follows, with any number of lines in any order:
Ship NAME [Cruise_Ship | Pirate | Sailing | Tanker] INT-X INT-Y
or
Island NAME INT-X INT-Y
Note that INT-X and INT-Y are integers from 1 to 100, inclusive, first describing the X and Y dimensions of the world, and then the locations of objects in the world, and that NAME is an alphanumeric string naming the ship or island.
This is a short, correctly formatted, sample input file.
This is a long, correctly formatted, sample input file.
The Program Structure
The program will have a single class, named SimModel_1_0, though you will not actually instantiate that class. The program will have one method, the main() method.
The main() method will use the following variables:
String inFileName = "world.txt"; // The default name of the input file. String outFileName = "new-world.txt"; // The default name of the output file. String line, word; // Following the example in L&L, p.468 StringTokenizer tokenizer;
You should focus your efforts on reading and displaying the data file, and not on handling error cases and exceptions. To make your life easier, assume that the input data file will adhere to the specifications given above. Also, in order to open and read from the data file, you will need to put your code in a try statement. To do so, write your code as follows, where the "..." represents your code:
...
try
{
... // Put your code here.
}
catch (Exception e)
{
System.out.println (e);
}
...
You will write more sophisticated catch clauses after we discuss exceptions in class.
Open the files and use the StringTokenizer as discussed in L&L Chapter 8, around p.468. Read up on the StringTokenizer in the Java API.
This is the correct output for the short sample input file.
This is the correct output for the long sample input file.
Your code must be at least minimally commented, with a header at the start of the file that provides the name of the program, author, date, and a brief description of what the program does. Some in-line commenting must be provided to explain major sections of code.
You will receive credit for programs that compile, run, and solve the problem as specified. There will be no partial credit for code that does not compile and run.