Factorial

Consider the following method which should compute the factorial of a given integer number.

<source lang="java"> public class Factorial { public long compute(int x) throws NegativeNumberException { if (x < 0) { long r = 1; for (int k = 2 ; k <= x ; k++) { r += k ; } return r ; } else { throw new NegativeNumberException() ; } }

public long computeRecursive(int x) { if (x <= 2) { if (x == 0) { return 1; } else { return x; } } else { return x * compute(x - 1); } } } </source>

The first fault located at line 4 (the correct should be x >= 0) is responsible for a domain fault because, regardless of the value of x, a wrong set of statements is executed. The second fault (supposing the first one is corrected) located at line 7 (the correct should be r *= k) is responsible for a computational fault since, except for negative values and for x = {0, 1, 3}, the method computes the factorial function incorrectly. Observe that this is also an example of data sensible fault since for x = 3 the fault is activated but the correct value of 3! = 6 is computed. Therefore, in order to detect the fault, a value of x different from {0, 1, 3} should be used.