软测hw3

一:(a)

对于图中表示的细节:

1int curPrime....curPrime=2

2~3:numPrimes<n

3:curPrime++;isPrime=true;

4:int i=0

5~6:i<=numPrimes-1

6~7:curPrime%primes[i]==0(T)

6~8:curPrime%primes[i]==0(F)

7:isPrime=false;break;

8:i++

5~9:i>numPrimes-1

9~10:isPrime(T)

9~11:isPrime(F)

2~12numPrimes>=n

12:int i=0

13~14:i<=numPrimes-1

14:system.out.println..

15:i++

13~16:i>numPrimes-1

 

(b) set MAXPRIMES=4, then t2 will get the resulting array bounds error

(c)n=1

(d)NC:TR={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

EC:TR={(1,2),(2,3),(2,12),(3,4),(4,5),(5,6),(6,7),(6,8),(7,9),(8,5),(5,9),(9,10),(9,11),(10,11),(11,2),(12,13),(13,14),(13,16),(14,15),(15,13)}

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

(e)[1,2,3,4,5,6,8,5,6,7,9,10,11,2,12,13,14,15,13,16]

(f)[1,2,3,4,5,6,8,5,6,7,9,10,11,2,12,13,14,15,13,16],[1,2,3,4,5,9,11,2,12,13,16]

二:基于JunitEclemma实现

 

被测试部分:

package sthw3;

 

public class PrintPrimes {
public String printPrimes (int n)
{
String prime=new String();
int curPrime; // Value currently considered for primeness
int numPrimes; // Number of primes found so far.
boolean isPrime; // Is curPrime prime?
int MAXPRIMES=100;
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 (int i = 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 (int i = 0; i <= numPrimes-1; i++)
{
prime+=primes[i]+" ";
}
return prime;
} // end printPrimes

 

}

测试部分:

package sthw3;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class PrintPrimestestTest {
private PrintPrimes print;
@Before
public void setup() throws Exception{
print=new PrintPrimes();

}
@Test
public void testPrintPrimes(){
assertEquals("2 3 5 7 11 ",print.printPrimes(5));
}
}

 

 

 

 

 

 

posted @ 2018-03-23 20:30  V_SWAN  阅读(114)  评论(0编辑  收藏  举报