CS 1120 Spring 2013 LA 4 (Part II) Critter Race

Lab Assignment 4 (Part II)
Critter Race

Note: This is a continuation of LA4, which is still due next week. This part will add a few additional requirements.

Concepts

·  Exception Handling

Problem Specification

You will be adding a feature to create a race based on input from a file. The first line of the input file should contain the characters representing the racetrack. The second line should indicate the number of racers. After that, each line represents an individual racer – the type of racer (monkey, turtle, etc.) and its name, separated by a comma. See the example input below.

Example Input:

######~~~~OO~~~~~~.....|

3

ostrich,Doduo

monkey,Mankey

turtle,Squirtle

You will also be adding exception handling to your application, which will primarily deal with handling invalid or missing input data. You will need to handle the following exceptions. The Design and Implementation phases below will discuss how.

·  FileNotFoundException

·  CritterRaceException (note: this exception and its subclasses will need to be created by you)

o  TrackFormatException

o  InvalidRacerException

Design Phase

Work with your partner, following the design process outlined here. Together, you must submit a hard copy of the Design Phase of your lab report by the end of lab today (only one copy needed for each pair).

Basic Structure

As always, first think about basic structure. In order to add a feature to create a race from a file, the IRace interface will need to require another method. Also, since now there is the potential for invalid input, it is possible that a race doesn’t actually get created. So the methods responsible for creating a race should return something indicating whether or not they were successful. On your lab report under basic structure, add the createRaceFromFile method to IRace and modify the return value of the createRace method as indicated below. Describe what each method does, what the parameters are for, and what the return values represent.

boolean createRaceFromFile(File inputFile);

boolean createRace(int length, int numRacers);

You will also see that the interface has one other new method – the getNumRacers method. This simply returns the number of racers in the race. It wasn’t included before since the GUI had that information already from the user – however, the problem with this becomes apparent when the race data comes from a file rather than the user. The GUI should rely on the Race class to supply all the information it needs to display a race.

For the exception handling, one of the exceptions you need to handle is already provided by Java libraries. However, the CritterRaceException class and its subclasses are specific to this situation, so you will need to create them yourself. Add these classes to the basic structure on your lab report.

Pseudocode

The steps below will ask you to write pseudocode or answer a question. Do this in your lab report.

  1. Write pseudocode for the createRaceFromFile method. Remember that with pseudocode, you should not use anything language-specific like Scanner or .nextLine(). Instead, use phrases like “open input file” and “read a line of input”. These can be translated into Java later. You can also leave out any exception handling for now.
  1. Exceptions are just classes and as such can utilize inheritance. The CritterRaceException class is a general exception class that inherits from Exception. It represents any type of exception related to the race, so it is marked as abstract and given to you already. The real work is in its subclasses (TrackFormatException or InvalidRacerException). For the following pieces of input, describe what is wrong and which of the two exceptions should be thrown:

a.  ######~~~~OO~~~~~~.....|

2

monkey,Curious George

human,Man in the Yellow Hat

b.  ######~~%~~OO~~~~~~....|

4

turtle,Leonardo

turtle,Donatello

turtle,Michaelangelo

turtle,Raphael

c.  ######~~~~OO~~~~~~.....

3

ostrich,Doduo

monkey,Mankey

turtle,Squirtle

  1. Unlike built-in exceptions, which may potentially be thrown by certain built-in methods, your custom exceptions will only be thrown if you explicitly do so. Revisit your pseudocode for the createRaceFromFile method and add steps that check for invalid input, throwing the appropriate exceptions. It should be able to handle situations like the invalid input above.
  1. The custom exceptions themselves are not very complicated. Each only needs a constructor that passes a String message up to the superclass constructor. This message is an error message which describes the particular exception. In order to create these messages though, the constructors need parameters which supply information about the problem. For each of the error messages below, indicate the information that needs to be passed in.
  1. Track string "######~~~~OO~~~$~~~.....|" is not in the correct format.
  1. Racer “Duck,Psyduck” is not a valid racer.

Implementation Phase

The implementation of the new feature and exception handling, along with the original requirements from last week, will be due in one week at the beginning of your lab.

Exception Handling

For this assignment, handling an exception means using try-catch or try-catch-finally blocks to print or log the error and then recover from it gracefully (e.g. return false or null, skip to the next input, or do whatever is most appropriate when that exception occurs).

As described in the Specifications above, in addition to the custom exceptions you create, you also need to handle FileNotFoundException. This should be caught wherever there is a danger of trying to use a file that may not exist. Likewise, the custom exceptions should be caught wherever they may possibly occur.

Normally in a catch block, you might write the error message to a log file, and then exit the method appropriately. There are convenient logging frameworks for this, such as in java.util.logging, but an explanation for how to use them would require more than a few example lines of code. So instead of writing to a log file, you may just print the exception’s error message to the standard error stream (usually the console), using System.err.println(). Feel free to use a logging framework, though, if you like.

The finally block is not necessary in every situation. It is most often used to close input streams (like Scanner) regardless of whether or not an exception was thrown.

Additional Notes

All other requirements, including the Testing and Maintenance phases, are described in the first part of this assignment. To test the exception handling, you should try using various input files with invalid input, including trying to open a file that doesn’t exist. You should include this when you discuss how you tested your application on your lab report.