First, the topic requirements
Use the following method Printprimes () to complete the subsequent question (a)-(d):
The code is as follows:
/******************************************************* * Finds and prints n prime integers * Jeff Offutt, SPR ING 2003 ******************************************************/ Public Static voidPrintprimes (intN) {intCurprime;//Value currently considered for primeness intNumprimes;//Number of primes found so far. BooleanIsPrime;//Is curprime prime? 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(inti = 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(inti = 0; I <= numPrimes-1; i++) {System.out.println ("Prime:" +Primes[i]); } } //End Printprimes
(a) Draw the Process Control chart
(b) Set Maxprimes to 4 so that t2= (n=5) will have an array out of bounds error, but t1= (n=3) has no effect.
(c) N=0 judgment will not meet the Numprimes < n cycle conditions, so will not enter the while loop
(d) Dot overlay: {1,2,3,4,5,6,7,5,6,8,9,10,11,12,13,14,15}
Side overlays: {(2,3), (2,11), (3,4), (4,5), (5,6), (6,7), (6,8), (7,5), (8,9), (5,9), (9,10), (9,2), (10,2), (11,12), (12,13), (13,14), (14,12), (12,15)}
Main path overrides: {(1,2,3,4,5,6,7), (1,2,3,4,5,6,8,9,10), (1,2,3,4,5,6,8,9), (1,2,3,4,5,9,10), (1,2,3,4,5,9), (1,2,11,12,13,14) , (1,2,11,15), (3,4,5,6,8,9,10,2,11,12,13,14), (3,4,5,6,8,9,2,11,12,13,14), (3,4,5,6,8,9,10,2,11,12,15), (3, 4,5,6,8,9,2,11,12,15), (3,4,5,9,10,2,11,12,13,14), (3,4,5,9,2,11,12,13,14), (3,4,5,9,10,2,11,12,15), (3, 4,5,9,2,11,12,15), (6,7,5,9,10,2,11,12,13,14), (6,7,5,9,2,11,12,13,14), (6,7,5,9,10,2,11,12,15), (6, 7,5,9,2,11,12,15), (13,14,12,15), (12,13,14,12), (5,6,7,5), (2,3,4,5,6,8,9,10,2), (2,3,4,5,6,8,9,2), (2, 3,4,5,9,10,2), (2,3,4,5,9,2)}
Attach: Implement one of the main path overrides:
Import Staticorg.junit.assert.*;ImportJava.io.ByteArrayOutputStream;ImportJava.io.PrintStream;ImportJava.lang.reflect.Method;Importorg.junit.* Public classTest {PrivatePrintprimes p; PrintStream Console=NULL; Bytearrayoutputstream bytes=NULL; @Before Public voidSetUp ()throwsException {p=NewPrintprimes (); Bytes=NewBytearrayoutputstream (); Console=System.out; System.setout (Newprintstream (bytes)); } @After Public voidTearDown ()throwsException {system.setout (console); } @Test Public voidTestResult ()throwsException {String s=NewString ("prime:2" + ' \ R ' + ' \ n ')); S+ = "Prime:3" + ' \ R ' + ' \ n '; S+ = "Prime:5" + ' \ R ' + ' \ n '; Class pp=P.getclass (); Method Method= Pp.getdeclaredmethod ("Printprimes", Newclass[]{int.class}); Method.setaccessible (true); Method.invoke (P,3); Assertequals (S, bytes.tostring ()); }}
Result diagram:
Software Test Exercise 7 after the third homework lesson