Download the folder EvictionsProject from the download directory. It contains these the two files of interest:
Study the code in this class, and finish up its implementation.
In particular do the following:
define the serialize() method to return a String representing the Town in the tab-separated form:
Town name state population poverty-rate evictions
In this task you will write a new class, named Evictions to do the following:
In your code you will have to deal with a couple of different kinds of exceptions. You should aim at providing use-friendly and informative messages when somethings goes wrong during the execution of your program.
Remember that any import statements should be added at the very top of the file. What are some of the Java classes to import to this class, given that we expect to read from and write to files? Of course you can come back to it later and add more imported classes as needed.
You can add more instance variables if you need. But remember that in general variables with a local scope should be kept local to the methods they are needed into.
The name of the input file should be an input parameter to this constructor.
Write a method, named readFromFile() to read all town data from a text file into the towns array.
We encourage you to open the input data file in an editor to examine its formatting. Notice that all pieces of data regarding a town is contained in one line. Also notice that the very first line in the file is a header, therefore it does not contain town data and it should be ignored.
Catch the relevant FileNotFoundException right here, in this method. Remember that in general it is a good idea to deal with any exception your code may produce as close to its source as possible!
Test this method so that you have some assurance that the file was read correctly into the towns array.
Write a method, named filterAndWriteToFile() which does exactly what its name indicates: it filters the towns according to the threshold, and writes, to a text file, every town with eviction rate greater than the threshold. The name of the output file should be passed as an input to this method.
We recommend, however, that you start with a simpler version of this method: Just write, into the output file, all towns contained in the towns array (i.e. no filtering in this first version).
Again, as you did in the reading method, deal with any produced exception right here, in this method. And remember to close the writer!
Test your method. How will you do so? What do you expect to see in your working folder after the method finishes execution? Verify your prediction.
Now improve this method so that only flagged towns are written into the output file. Check the methods available in the Town class to see how you can tell whether a town is flagged or not.
Some times input data may not be formed as expected, and this may have consequences in the way the program runs. Consider your Evictions program:
Feel free to add any helper methods if you think it is appropriate.