Question: Part A: Draw the control flow graph for the printPrimes() method. Part B: Consider test cases t1 = (n = 3) and t2 = (n
Part A: Draw the control flow graph for the printPrimes() method.
Part B: Consider test cases t1 = (n = 3) and t2 = (n = 5). Although these tour the same prime paths in printPrimes(), they do not necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.
Part C:For printPrimes(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.
Code:
// Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 7; page ?? // Can be run from command line // See PrintPrimes.num for a numbered version. // No JUnit tests at this time
/** ***************************************************** * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ********************************************************* */ public class PrintPrimes {
private static boolean isDivisible (int i, int j) { if (j%i == 0) return true; else return false; } private static void printPrimes (int n) { 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]); } } // end printPrimes public static void main (String []argv) { // Driver method for printPrimes // Read an integer from standard input, call printPrimes() int integer = 0; if (argv.length != 1) { System.out.println ("Usage: java PrintPrimes v1 "); return; } try { integer = Integer.parseInt (argv[0]); } catch (NumberFormatException e) { System.out.println ("Entry must be a integer, using 1."); integer = 1; } printPrimes (integer); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
