Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
hi im making a class that has methods to calculate prime numbers, im having a problem modifying the getNthPrime method to call checkAndResize method which makes the array(sieve) expand if the primenumbers object has a smaller parameter than what the getNthPrime method's parameter is. How can i do this, I keep getting a fatal exception or an arrayOutofboundsexception(which makes sense).

Code:
package primes;

public class PrimeNumbers {
    private boolean [] sieve;
    public PrimeNumbers(int  upper) {
        initializeSieve(upper);
    }

    public int getNthPrime (int n){
    int prime =0;
    int position = -1;

    while (prime < n )
    {
        position ++;
        if (sieve[position])
            prime++;
    }
        return position;
    }

    public int getNumberPrimeNumbers(int n){
        int primes = 0;
        for (int i =0 ; i < sieve.length ; i ++){
            if (n > sieve.length){
                checkAndResize(n);
                initializeSieve(n);
                    if(sieve[i])
                        primes++;
            }
            else if (sieve[i])
                primes++;
        }
        return primes;
    }
    public int getSieveSize ()
    {
        return sieve.length;
    }

    public boolean isPrime (int n) {
        if (n > sieve.length){
                checkAndResize(n);
                initializeSieve(n);
            }

        return sieve[n];
    }

    // prints out the prime numbers inside sieve
    public void printPrimeNumbers() {
        int n = 0;
        boolean first = true;
        System.out.print("[");
        for(int i = 0; i < sieve.length - 1; i++){
            n++;
            if(sieve[i] == true && n != sieve.length - 1) {
                if(first) first = false;
                else System.out.print(" ");
                System.out.print(i);
            }
        }
        System.out.print("]");
    }

    // checks length of sieve  with N and then resizes sieve if nessecary.
    private void checkAndResize (int n){
            if ((n + 1) > sieve.length){
            initializeSieve(2*n);
         }
    }
    private void setMultiples (int k) {
        for (int i = 2*k; i < sieve.length; i += k)
            sieve [i] = false;
    }

    private void initializeSieve (int upper){
        if ( upper < 2)
            sieve = new boolean [2];
        else
            sieve = new boolean [upper + 1];

        sieve[0] = false;
        sieve[1] = false;

        for (int i =2 ; i< sieve.length; i ++ )
            sieve[i] = true;

        int bound = (int) Math.ceil(Math.sqrt(sieve.length));
        for (int i = 2 ; i < bound ; i ++)
            if (sieve[i])
                setMultiples (i);


    }

    private String booleanToString (boolean value)
    {
        if (value)
            return "T";
        else
            return "F";
    }
    public String toString (){
        StringBuffer buf = new StringBuffer("[");
        for (int i = 0; i < sieve.length -1 ; i ++)
            buf.append(booleanToString (sieve[i]) + " " );

        buf.append(booleanToString (sieve[sieve.length -1]) + "]");
        return buf.toString();
    }

}

thanks in advance:apple:
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Array's in Java aren't mutable so don't expand and are a fixed size, if you want them to expand them as you seem to here you need to use an ArrayList, you will need to store the numbers Integer's instead of int's. (or Boolean's in this case)

Btw S/he probably knows where you go to school because s/he's in the same class ;).
 

dcr

macrumors member
Jun 10, 2002
57
0
um.... how do you know where i go to school? lol and better yet my class.

Something to think about: if a someone visiting macrumors can find your instructor, your instructor (and grader) can probably find macrumors. :)
 

CharlesRobinson

macrumors newbie
Jan 27, 2006
23
0
Something to think about: if a someone visiting macrumors can find your instructor, your instructor (and grader) can probably find macrumors. :)
No less, that's a pretty decent display of the flow of information.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.