Given this recursion method public static int sumuint s ifs

Given this recursion method:

public static int sumu(int s) {

if(s ==0)

return 0;

else

return s + sumu(s - 1);

}

a- What problem could potentially occur with the above definition?

b- Rewrite the method so that it will throw an ArithmeticException if the problem from question a occurs.

Solution

The only problem with above definition is it will result iin never ending process if value less than zero is passed as an first arguement.

To correct that add below code.

        if(s<0)
            throw new ArithmeticException();

Given this recursion method: public static int sumu(int s) { if(s ==0) return 0; else return s + sumu(s - 1); } a- What problem could potentially occur with the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site