首页 > 代码库 > HW03:Exercise Section 2.3

HW03:Exercise Section 2.3

1)绘制程序的控制流图:

 

 1 public class Print {
 2     private static final int MAXPRIMES = 100;
 3     /******************************************************* 
 4      * Finds and prints n prime integers 
 5      * Jeff Offutt, Spring 2003 
 6      ******************************************************/ 
 7     public static void printPrimes (int n) 
 8     { 
 9         int curPrime; // Value currently considered for primeness 
10         int numPrimes; // Number of primes found so far. 
11         boolean isPrime; // Is curPrime prime? 
12         int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 
13         
14         // Initialize 2 into the list of primes. 
15         primes [0] = 2; 
16         numPrimes = 1; 
17         curPrime = 2; 
18         while (numPrimes < n) 
19         { 
20             curPrime++; // next number to consider ... 
21             isPrime = true; 
22             for (int i = 0; i <= numPrimes-1; i++) 
23             { // for each previous prime. 
24                 if (isDivisable(primes[i],curPrime)) 
25                 { // Found a divisor, curPrime is not prime. 
26                     isPrime = false; 
27                     break; // out of loop through primes. 
28                 } 
29             } 
30             if (isPrime) 
31             { // save it! 
32                 primes[numPrimes] = curPrime; 
33                 numPrimes++; 
34             } 
35         } // End while 
36         
37         // Print all the primes out. 
38         for (int i = 0; i <= numPrimes-1; i++) 
39         { 
40             System.out.println ("Prime: " + primes[i]); 
41         } 
42     } // end printPrimes
43     }

技术分享


B)当n=3时输出的数组中存在2,3,5

    当n=5时输出的数组中存在2,3,5,7,11

    因此当MAXPRIMES数值为1的时候数组越界错误;

C)t=(n=1)

D)

点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

边覆盖:{(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(9,10),(10,11),(9,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}

主路径覆盖:{(1,2,3,4,5,6,7),

(1,2,3,4,5,6,8,9,10,11),

(1,2,3,4,5,6,8,9,11),

(1,2,12,13,14,15),

(1,2,12,13,16),

(1,2,3,4,5,9,10,11),

(1,2,3,4,5,9,11),

(3,4,5,6,7),

(3,4,5,6,8,9,10,11,2,12,13,14,15),

(3,4,5,6,8,9,10,11,2,12,13,16),

(3,4,5,6,8,9,11,2,12,13,14,15),

(3,4,5,6,8,9,11,2,12,13,16),

(3,4,5,9,10,11,2,12,13,14,15),

(3,4,5,9,10,11,2,12,13,16),

(3,4,5,9,11,2,12,13,14,15),

(3,4,5,9,11,2,12,13,16),

(6,7,5,9,10,11,2,12,13,14,15),

(6,7,5,9,10,11,2,12,13,16),

(6,7,5,9,11,2,12,13,14,15),

(6,7,5,9,11,2,3,4),

(6,7,5,9,10,11,2,3,4),

(6,7,5,9,11,2,3,4),

(14,15,13,16),

}

 附加:基于JunitEclemmajacoco)实现一个主路径覆盖的测试。

测试代码:

 技术分享

 

测试结果:

技术分享

技术分享

技术分享

 

HW03:Exercise Section 2.3