HW03:Exercise Section 2.3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

usaco-Section 2.3-Controlling Companies

将千克转换成磅 Exercise05_03

系统分析与设计HW9

tomcat 报错出现 jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Ser

03JavaScript程序设计修炼之道 2019-05-23_20-28-51_2019-05-26_21-12-07 for练习hw

jar 未加载。请参阅 Servlet 规范 2.3,第 9.7.2 节。违规类:javax/servlet/Servlet.class