There was a question in my book that said this (there are no answers):
Suppose we have a class Beta declared with the header:
class Beta extends Alphaso Beta is a subclass of Alpha, and in class Alpha there is a method with header:
public int value(Gamma gam) throws ValueExceptionWrite a static method called addValues which takes an object which could be of type
ArrayList<Alpha>orArrayList<Beta>, and also an object of typeGamma. A call to the method addValues must return the sum obtained by adding the result of a call of value on each of the objects in the ArrayList argument, with the Gamma argument as the argument to each call of value. Any call of value which causes an exception of type ValueException to be thrown should be ignored in calculating the sum.
My Attempt:
public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) {
int sum = 0;
for (int i = 0; i < arr.size(); i++) {
try {
sum += arr.get(i) + gam;
} catch (Exception e) {
i++;
}
}
return sum;
}
Although I know for starters that the line sum += arr.get(i) + gam is going to give me an error, because they are not straight forward ints that can be added. The book provides no more information on the question so what I have written here is everything required for the question.