We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
//Mine is in JavaclassCalculator{// it seems to be a utility, make it staticstaticintpower(intn,intp)throwsException{if(n<0||p<0){thrownewException("n and p should be non-negative");}// wonder if "return (int) Math.pow(n,p)" is cheating// I'll do it the old fashion way - recursion// even 0 to the 0th power is still 1if(p==0){return1;}/* * this n==0 block below is not necessary * just to save some computational time * when n is 0 (and p is big) */if(n==0){return0;}returnpower(n,p-1)*n;}}
Day 17: More Exceptions
You are viewing a single comment's thread. Return to all comments →