CS230 Lab: on Exceptions and IO

Lab: on Exceptions and IO

File IO and Exceptions

 

Solutions

 

Recursion Practice

Go to the CodingBat site, and write code to solve the array11 and count7 problems.

 

Getting started

Download the lab5 folder to your Desktop. Rename it to include the names of the two partners.

 

Task 1: Town class

Study the Town.java given code. Finish up the implementation of this class. In particular do the following:

  • define the toString() method to return a reasonable String representation of a Town object.
  • define the compareTo() method to implement the Comparable Interface. Town comparison, for our purposes, should happen based on the eviction rates: a Town with larger eviction rate than an other one is considered "larger".
  • define the equals() method. Two towns are equal when they have the same name and the same state.
  • add a main() method, with some code to test your work
  • finally, add the necessary javadoc to your code

[end of PRELAB]

 

Task 2: IOExceptions Reading from and Writing to Text Files

Relevant downloaded files:

  • Town.java
  • evictionData.txt

Task 2A: WatchingTowns.java

Write a program, from scratch, that will read in a file of town data regarding evictions in a certain year, and decide which of those towns should be on the watch because eviction data are raising concerns. It will write these town-under-watch to a file.

A town is considered to be "on-the-watch" if its eviction rate is greater than some threshold. This threshold is the same for all Towns, obviously. Provide means of setting it to different values.

Input is coming from the file named evictionData.txt. Open the file in a text editor, and examine its formatting. Notice that the first line of the file contains headers (no data.)

The program should do the following:

  • read all town data into an array of Town objects
  • decide which towns should be on the watch, and mark them accordingly
  • save all those towns-on-the-watch in an other array
  • finally, write the towns-on-the-watch into an output text file

Some details:

  • name your class WatchingTowns
  • what are some of the java classes to import to this class?
  • what are some instance variables to include in this class?
  • pass the name of the input and output files as input parameters to the constructor. This way it will be easier to adjust your program to read from and/or write to an other file.
  • use while-loop(s) and for-loop(s) in your code.
  • remember to close any scanner/writer after you are done with them.

Task 2B: Malformed input data: Exceptions

  • How do you expect your program to behave if the input and/or output file does not exist? How can you test your answer to the previous question? Go ahead, do so!

  • How about if the data itself is malformed? For example, what happens if one town's population or/and evictions number, or/and poverty rate is not a proper number (int or double)? Temporarily edit the input data file so that it contains some malformed data to test your hypothesis.

  • Use try and catch statements and print an informative message about what went wrong in the execution of this program. Be sure to test each of the exceptions as well. Of course, feel free to add more data to the input file, or modify the existing ones for your testing needs. If one record is problematic, how is your program dealing with the records that follow after it?

Task 2C: Using Exceptions for Recovery

This is getting really interesting, right? Think about how you can modify your program so that when an invalid town record is encountered, the whole record is ignored, but the reading continues with the next student record. Can you arrange so that the problematic record is just skipped, a message is presented, and the program continues with the next record?