CS230 Exceptions, IO: Towns and Evictions

Exceptions, IO: Towns and Evictions

Download the folder EvictionsProject from the download directory. It contains these the two files of interest:

  1. Town.java
  2. smallEvictionData.txt

Task 1: Town class

Study the code in this class, and finish up its implementation.

In particular do the following:

  • define the equals() method according to the following logic: two town objects are considered equal if and only if they have the same name and belong to the same state.
  • define the compareTo() method so that it implements the Comparable Interface based on the towns' eviction rates. For example, a town object compared with another town object is considered smaller if its eviction rate is smaller than the other town object's eviction rates. Hint: Recall how we compared Shape objects in a previous lab. Read the comment about the role of SMALL_NUM.
  • define the serialize() method to return a String representing the Town in the tab-separated form:

    Town name state population poverty-rate evictions

Task 2: IOExceptions: Reading from and writing to text files

In this task you will write a new class, named Evictions to do the following:

  • Read town data from an input file into memory. Use an array to hold the Town objects.
  • Process the data in the Town array so that to flag all towns with an increased eviction rate (depending on a provided threshold).
  • Write the flagged town objects into an output file.

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.

Task 2A: Setting up the Evictions class

Import java classes

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.

Set up your instance variables:
  1. Declare an array of towns, named towns.
  2. Declare a variable (double), named threshold: every town with eviction rate greater that this threshold should be flagged during the processing phase.
  3. Lastly, declare the name of the file your program will read data from. Name this variable inFileName.

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.

Write the constructor

The name of the input file should be an input parameter to this constructor.

Task 2B: Reading data from a file

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.

Task 2C: Writing to a file

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.

Task 3: Malformed input data: Exceptions and Recovery

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:

  • What may happen if the number of evictions for a town is not a proper number?
  • How about if the poverty rate is not a proper number?
  • How do you test your code against cases like the above? Edit the input data file so that it contains some malformed data to run your program and test your hypothesis.
  • How can you change your code so that when such a malformed piece of input is encountered the program behaves in a graceful way? Arrange so that your program simply ignores the line of invalid data, and continues on to the next line.

Feel free to add any helper methods if you think it is appropriate.