at many prime numbers. Iterator() returns an Iterator object that will iterate through the primes, and toString() returns a string representation.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

 

Consider the following class PrimeNumbers, which has three methods. The first, computePrimes() takes one integer input and calculates that many prime numbers. Iterator() returns an Iterator object that will iterate through the primes, and toString() returns a string representation.

 

public class PrimeNumbers implements Iterable<Integer>

{

private List<Integer> primes = new ArrayList<Integer>();

public void computePrimes (int n)

{

int count = 1; // count of primes

int number = 2; // number tested for primeness

boolean isPrime; // is this number a prime

while (count <= n)

{

isPrime = true;

for (int divisor = 2; divisor <= number / 2; divisor++)

{

if (number % divisor == 0)

{

isPrime = false;

break; // for loop

}

}

if (isPrime && (number % 10 != 9)) // FAULT

{

primes.add (number);

count++;

}

number++;

}

}

 

@Override public Iterator<Integer> iterator()

{

return primes.iterator();

}

 

2

 

@Override public String toString()

{

return primes.toString();

}

}

 

computePrimes() has a fault that causes it not to include prime numbers whose last digit is 9 (for example, it omits 19, 29, 59, 79, 89, 109, ...).

 

Normally, this problem is solved with the Sieve of Eratosthenes. The change in algorithm changes the consequences of the fault. Specifically, false positives are now possible in addition to false negatives.

 

(a) Reimplement the algorithm for calculating primes using the Sieve of Eratosthenes approach, but leave the fault in place.

 

(b) What is the first false positive and how many primes must a test case generate before encountering it?

 

(c) A test must reach a location in a program that contains the fault (reachability). After the location is executed, the state of the program is incorrect (infected). The infected state must propagate through the execution and cause some output of the final state of the program to be incorrect (propagation). Finally, the tester must observe part of the incorrect portion of the final program state (revealability). (This all serves to illustrate that testing is actually really complicated!) What does this example illustrate about these four concepts

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning