Homework3_3015218122_戎达

(a)

 

 

(b)令MAXPRIMES=3,这时n=5时会发生数组越界,而n=3时不会发生。

(c)n=1

(d)点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12,13}

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

主路径覆盖:{(5,6,8,5),(6,8,5,6),(8,5,6,8),(1,2,10,11,13),(7,2,10,11,13),(1,2,10,11,12,13), (6,7,2,10,11,12,13),(8,5,7,2,10,11,13),(6,7,9,2,10,11,12,13),(6,8,5,7,2,10,12,13),(3,4,5,6,7,2,10,11,13),(3,4,5,7,9,2,10,11,13),(3,4,5,7,2,10,11,12,13),(6,8,5,7,9,2,10,11,13),(6,8,5,7,2,10,11,12,13),(3,4,5,7,9,2,10,11,12,13), (4,5,6,7,9,2,10,11,12,13), (6,8,5,7,9,2,10,11,12,13), (8,5,6,7,9,2,10,11,12,13), (3,4,5,6,7,9,2,10,11,12,13),}

主路径覆盖的测试:

(1)首先修改原程序Prime.java,将其修改成有返回值类型的方法,返回其输出的内容

public class Prime {
          public static boolean isDivisible(int a,int b){
              if(b%a==0){
                  return true;
              }
            return false;
              
          }
          public static String printPrimes (int n)
          {
              String m = "";
              int curPrime; // Value currently considered for primeness
              int numPrimes; // Number of primes found so far.
              boolean isPrime; // Is curPrime prime?
             int [] primes = new int [100]; // 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 (isDivisible(primes[i], curPrime))
                     { // 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++)
             {
                 System.out.println ("Prime:" + primes[i]);
                 m=m+"Prime:" + primes[i];
             }
             System.out.println(m);
            return m;
         } // end printPrimes
}

(2)写测试方法hw3Test.java

public class hw3Test {
  Prime p=new Prime();
  @Test
  public void TestPrime(){
      Assert.assertEquals("Prime:2Prime:3Prime:5Prime:7Prime:11", p.printPrimes(5));
      Assert.assertEquals("Prime:2Prime:3Prime:5Prime:7Prime:11Prime:13Prime:17Prime:19", p.printPrimes(8));
  }
}
(3)测试结果如下:

 

 

posted on 2018-03-25 13:54  IamRadar  阅读(93)  评论(0)    收藏  举报

导航