// MathStuff // Functional class for practice exam 2 // Kevin Bierre public class MathStuff { // this method calculates the factorial of a number // and returns it to the calling routine public int factorial(int num) { // loop to calculate the factorial value boolean negative = false; if(num < 0) { negative = true; // flag as negative value num = Math.abs(num); // get positive value } int result = 1; for(int i = 1; i <= num; i++) { result *= i; } // fix negative value if(negative) { result *= (-1); // multiply by -1 } // return result return result; } // this method calculates the summation of a number // and returns it to the calling routine public int summation(int num) { // loop to calculate the summation value boolean negative = false; if(num < 0) { negative = true; // flag as negative value num = Math.abs(num); // get positive value } int result = 0; for(int i = 0; i <= num; i++) { result += i; } // fix negative value if(negative) { result *= (-1); // multiply by -1 } // return result return result; } }