Question

The PrimeDetector class shown below has documentation which describes the uses of each method. The tester class creates an object of the PrimeDetector class and attempts to print a set of generated integer values from an ArrayList returned from the hasPrime() method. I do not see my error, as I have created a procedural version of it which works just fine. It prints only the first 3 prime numbers, and returns as having only detected 3 prime numbers--leading me to believe that the issue lies within the PrimeDetector class, somewhere within the for-loop, though I can't be certain, as it is nearly the same structure as my procedural version--so far as I can tell. I will include the single procedural class, as well as the OOP version and its tester.

/**
 * The PrimeDetector class detects prime numbers within a user's
 * given set [0,n] where n is a user-given upper limit.
 * 
 * @author A. Mackey
 * @version 06/05/14
 */
import java.util.*;
public class PrimeDetector {
    private int n;
    private int primeCounter;
    private ArrayList<Integer> primeList = new ArrayList<Integer>();

    /**
     * Constructor for objects of class PrimeDetector
     * @param n is the upper limit in the set [0,n] tested with the hasPrime() method.
     */
    public PrimeDetector(int n) {
        this.n = n;
    }

    /**
     * @return an ArrayList of type Integer containing all prime values within the set [0,n].
     */
    public ArrayList<Integer> hasPrime() {
        primeCounter = 0;
        for (int i = 1; i <= n; i++) {
            boolean isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                primeCounter++;
                primeList.add(i);
            } else {
                break;
            }
        }
        return primeList;
    }

    /**
     * @return primeCounter variable which holds and integer value equivalent to the number of prime values in
     * the [0,n] set evaluated in the hasPrime() method.
     */
    public int getPrimeCounter() {
        return primeCounter;
    }
}

Tester:

/**
 * The PrimeDetectorTest class tests the PrimeDetector class, which detects prime numbers within a user's
 * given set [0,n] where n = a user-given upper limit.
 * 
 * @author A. Mackey
 * @version 06/05/14
 */
import java.util.*;
public class PrimeDetectorTest
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a positive integer you wish to find primes up to: ");
        int n = in.nextInt();
        System.out.println("The following list is prime within the range [0, " + n + "]: ");
        PrimeDetector list = new PrimeDetector(n);

        ArrayList<Integer> primeList = list.hasPrime();

        for (int s : primeList)
        {
            System.out.println(s + " is prime.");
        }

        System.out.println(list.getPrimeCounter() + " prime numbers within this set.");
    }
}

Procedural version:

import java.util.*;
public class PrimeDetectorV1
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a positive integer you wish to find primes up to: ");
        int n = in.nextInt();
        int primeCounter = 0;
        System.out.println("The following list is prime within the range [0, " + n + "]: ");

        for(int i = 0; i <= n; i++)
        {
            while (i>0)
            {
                boolean isPrime = true;
                for (int j = 2; j <= i/2; j++)
                {
                    if(i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    System.out.println(i + " is prime.");
                    primeCounter++;
                    break;
                }
                else
                {
                    break;
                }
            }
        }
        System.out.println("There are " + primeCounter + " prime numbers within this set.");
    }
}
Was it helpful?

Solution

  if (isPrime) {
      primeCounter++;
      primeList.add(i);
  } else {
       break;
  }

Remove the else{break;} here. Because if you found a number that is not prime, you still need to check the next one. Currently your program will stop when it founds a number that is not prime.

Also 1 is not a prime number so you should start your first loop at 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top