cs2110.assignment1

Interface SpeciesReader

public interface SpeciesReader

Class to read a CS2110 species data file. Species data files typically end with the extension ".dat", and contain a list of "commands", like this:

Name="Strats_Squirrel"

LatinName="Leapus Longus"

Image="Strats_Squirrel.bmp"

DNA="AIIOOOIICOCCICOCAOOAICOAIOCCOOCCACOOOIACOACACOAIACIOAOOIICOIICCIICICO"

The DNA string will be longer than the one shown; ours have about 50,000 characters each. The set of data files is in a zip file you should download from the project page, along with the set of animal images (one bmp file per animal). Your solution should ignore extra white spaces (blanks or tabs), for example in the line

Name = "Strats_Squirrel" It should also handle blank lines and lines with commands other than the ones shown above (but always in the identical format command_name = "value")

Not shown but required: The constructor for a class implementing SpeciesReader should take a single string argument, the file name, and open the corresponding file. An IOException should be thrown if any error occurs. After opening a file, the constructor may want to retain the file handle for use by other methods

Thus, the constructor of class MySpeciesReader implementing SpeciesReader should have a declaration like this:

public MySpeciesReader(String fileName) throws IOException

Clarification

The constructor should position SpeciesReader at the first line, such that getCommand() and getArgument() will return the first command and argument. I.e., this code should work:

SpeciesReader reader = new MySpeciesReader(filename);
System.out.println("The first command is: " + reader.getCommand());
Note that the user in this case does not call readNextLine() first.

This definition should replace the automatically generated one that Eclipse will produce if you tell it that you wish to implement the SpeciesReader

Also not shown: A file reader should "remember" the file handle of the current open file and the contents of the current line of the file, for use in the Command() and Argument() methods.

Hint: We find the BufferedReader class (part of the JDK) useful for reading a file one line at a time. You don't have to use it, but that's what we used in our solution. If you do use BufferedReader, Eclipse will add some "include" lines and will force you to write code to handle exceptions. This is good -- one should always handle every condition that can arise, such as exceptions, and we expect your code to do so.

We recommend that you create a class, Constants, and that all constants be static final members in that class. Then you can refer to them by Constants.XYZ in your code, and if for some reason a constant turns out to have been poorly chosen, it will be easy to change without needing to hunt for all the places you use it.

Not shown: The finalize() method for classes implementing this interface should close the file
Update: Use of finalize to close streams should be avoided.  There is no guarantee that the finalize method will ever be called by the JVM.
The preferred method is to close the your file input stream immediately after you're done reading the file. 
On this assignment, either technique is acceptable.

The class itself should "remember" the current open file until finalize() is called

Method Summary

String
getArgument()
Returns the argument inside the quotes associated with the current command.
String
getCommand()
CS2110 data files contain commands.
boolean
readNextLine()
Advance to the next line of the file

Method Details

getArgument

public String getArgument()

getCommand

public String getCommand()

readNextLine

public boolean readNextLine()