Your last name (BLOCK CAPITALS): _______________ first name: __________
Two-character quiz section ID (if you are registered): ______
CSE143
Miniquiz #05 July 16, 2003
The method below takes a total and a count (as strings) and returns the average. A number of errors are possible when the method runs. Modify the method so that in case of any error at all, including a violated precondition, an IllegalArgumentException is thrown. The method should not throw any other exceptions or indicate errors in any other way.
Note 1: java.lang.IllegalArgumentException is unchecked.
Note 2: valueOf throws a NumberFormatException if the string it is given is not a valid number.
/** Compute the average, defined as total divided by count.
* Preconditions: total and count must not be null and must be valid numbers, and both must be > 0. */
public static double average(String total, String count) {
Double totD = Double.valueOf(total);
double tot = totD.doubleValue();
Integer numD = Integer.valueOf(count);
int num = numD.intValue();
return tot/num;
}