CPSC 170 Lab 4: Exceptions

Placing Exception Handlers

The file ParseInts.java contains a program that does the following:

Save ParseInts to your directory, compile and run it. If you give it the input:

    10 20 30 40

It should print:

    The sum of the integers on the line is 100.

Now try a line that contains both integers and other values, for example:

    We have 2 dogs and 1 cat.

You will get a NumberFormatException when it tries to call parseInt on "We", which is not an integer. One way around this is to put the loop that reads inside a try block and catch the NumberFormatException, but not do anything with it. This way if it's not an integer it doesn't cause an error; it goes to the exception handler, which does nothing. Do this as follows:

Throwing Exceptions

The file Factorials.java contains a program that calls the factorial method of the MathUtils.java class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers. Returning 1 as the factorial of any negative integer is not correct mathematically, the factorial function is not defined for negative integers. To correct this, you could modify your factorial method to check if the argument is negative, but then what? The method must return a value, and even if it prints an error message, whatever value is returned could be misconstrued. Instead, it should throw an exception indicating that something went wrong so it could not complete its calculation. You could define your own exception class, but there is already an exception appropriate for this situation IllegalArgumentException, which extends RuntimeException. Modify your program as follows:

  1. Modify the body of factorial to check the value of the argument and, if it is negative, throw an IllegalArgumentException. Note that the expression that follows throw is actually an instance of the IllegalArgumentException class, and that the constructor takes a String parameter. Use this parameter to be specific about what the problem is.
  2. Compile and run the Factorials program after making these changes. Now when you enter a negative number an exception will be thrown, terminating the program. The program ends because the exception is not caught, so it is thrown by the main method, causing a runtime error.
  3. Modify the main method in your Factorials class to catch the exception thrown by factorial and print an appropriate message, but then continue with the loop. The message should include only the message that you passed with the exception, not the exception itself!

Returning a negative number for values over 16 also is not correct. The problem is arithmetic overflow. The factorial is bigger than can be represented by an int. This can also be thought of as an IllegalArgumentException this factorial method is only defined for arguments up to 16. Modify your code in factorial to check for an argument over 16 as well as for a negative argument. You should throw an IllegalArgumentException in either case, but pass different messages to the constructor so that the problem is clear.

File I/O

Remember the CD Collection from program from lab last semester? The program had a command line interface that allowed you to add CDs to a collection and perform searches on the collection. If you don't remember you can Download the files Tunes.java, CDCollection.java, and CD.java in order to remind yourself. One problem with this program is that it each time it is run the collection is reinitialized to the same default collection. The program does not have a persistent state, so you are going to add one. Add to this program the ability to read and write a CD collection from a text file.

The file representation of a CD collection for this program will list the title, artist, cost, and number of tracks each on a separate line. This will allow you to use the nextLine method of the Scanner class to parse the file. The file ExampleCollection.txt is an example of a collection text file. You will need to do the following:

  1. Add a prompt when the program first runs that asks the user to enter a file name of a CD collection.
  2. Replace the CDCollection constructor with a constructor that takes a string that is the name of a file that contains a CD collection.
  3. Replace the code in the Tunes class that initializes the Collection with 6 CDs to call the CDCollection constructor with the file name specified by the user.
  4. Add a write method to the CDCollection class.
  5. Add code to the Quit case in the Tunes class so that before exiting the collection is written to the file that was opened. This code should utilize a try/catch block to inform the user if there is an error when writing the file.

Submission

Submit a zip file of your code on the course Inquire site that uses your last names as the file name.