/******************************************************* * Finds and prints n prime integers * Jeff Offutt, SPR ING 2003 ******************************************************/ Public Static voidPrintprimes (intN) {intCurprime;//Value currently considered for primeness intNumprimes;//Number of primes found so far. BooleanIsPrime;//Is curprime prime? int[] primes =New int[Maxprimes];//The list of prime numbers. //Initialize 2 into the list of primes.primes [0] = 2; Numprimes= 1; Curprime= 2; while(Numprimes <N) {curprime++;//next number to consider ...IsPrime =true; for(inti = 0; I <= numPrimes-1; i++) { //For each previous prime. if(curprime%primes[i]==0) { //Found a divisor, curprime is not prime.IsPrime =false; Break;//Out of loop through primes. } } if(isprime) {//Save It!Primes[numprimes] =Curprime; Numprimes++; } } //End while//Print all the primes out. for(inti = 0; I <= numPrimes-1; i++) {System.out.println ("Prime:" +Primes[i]); } } //End Printprimes
Title Code as above, Method printprimes ()
(a) Control flow graph
(b) When an array out-of-bounds error occurs, it is easier to consider test case t1= (n=3) and t2= (n=5) at this time T2 than T1 to find errors.
(c) It is possible to pass the while loop when N=1.
(d)
Node overwrite: {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
Side overlays: {(2,3), (2,12), (3,4), (4,5), (5,6), (5,9), (6,7), (6,8), (8,9), (7,5), (9,10), (9,11) (The 10,11), (11,2), (12, 13), (13,14), (14,13), (13,15)}
Main path Overrides:
[1,2,3,4,5,6,7] ;
[1,2,3,5,6,8,9,11];
[1,2,3,5,6,8,9,10,11];
[1,2,3,5,9,11];
[1,2,3,5,9,10,11];
[1,2,12,13,14];
[1,2,12,13,15];
[3,4,5,6,8,9,10,11,2,12,13,14];
[3,4,5,6,8,9,10,11,2,12,13,15];
[3,4,5,6,8,9,11,2,12,13,14];
[3,4,5,6,8,9,11,2,12,13,15];
[3,4,5,9,11,2,12,13,15];
[3,4,5,9,11,2,12,13,14];
[3,4,5,9,10,11,2,12,13,15];
[3,4,5,9,10,11,2,12,13,14];
[6,9,5,9,11,2,12,13,15];
[6,9,5,9,11,2,12,13,14];
[6,9,5,9,10,11,2,12,13,15];
[6,9,5,9,10,11,2,12,13,14];
[14,13,15];
[2,3,4,5,6,8,9,11,2];
[2,3,4,5,6,8,9,10,11,2];
[2,3,4,5,9,11,2];
[2,3,4,5,9,10,11,2];
[13,14,13];
[5,6,7,5];
The seventh question of Homwork4--graphcoverage