CS
1401 Introduction to Computer Science
Fall 2013, Lab 12
Objective. The purpose of this lab is to practice
exceptions.
Background. In your Labs 9-11, you designed methods to deal with fractions.
In these labs, we did not require you to take into account:
- that
a denominator of a fraction cannot be 0, and
- that it is not possible to divide by a fraction which is equal to 0.
Assignment. Modify your Lab 10 so that when in both 0 cases, a proper exception is raised, and
a proper action is performed. Specifically:
- make the Fraction constructor throw an IllegalArgumentException (or a custom Exception as explained in Section 14.9, for extra credit);
- make the division method in the Fraction class throw a similar exception if the divisor is zero.
In the main method:
- prompt the user to provide the numerator and the denominator
for two fractions and an operator,
- create the fraction objects,
- apply the operator, and
- print the result.
Catch the exceptions thrown by the Fraction methods and print appropriate error messages.
In addition, if the user provides invalid values for a numerator and/or a denominator, catch the exception and print an appropriate error message.
For extra credit. Make a similar modification of Lab 11. Do the same thing as in modification of Lab 10, except that:
- the fractions and operators are read from the appropriate text fields, and
- the answer is written in other appropriate text fields.
Write the error message either in a text field, or in a dialog box (using JOptionPane, as in Section 1.9).
When it is due. Lab 12 is due at the beginning of the
first lab section on the week of November 25, i.e.:
- on
Monday November 25 for those who attend Monday-Wednesday labs, and
- on Tuesday November 26 for those who attend Tuesday-Thursday
labs.