need help modifying one method of a class in java

Discussion in 'Mac Programming' started by macman2790, Mar 3, 2007.

  1. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #1
    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:
     
  2. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    Is this for a school assignment, so we should give you tips but not finished code?
     
  3. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #3
    yes i'm asking for tips. thanks, but anything will help
     
  4. dcr
    macrumors member

    Joined:
    Jun 10, 2002
    #4
  5. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #5
  6. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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 ;).
     
  7. dcr
    macrumors member

    Joined:
    Jun 10, 2002
    #7
    Something to think about: if a someone visiting macrumors can find your instructor, your instructor (and grader) can probably find macrumors. :)
     
  8. macrumors newbie

    CharlesRobinson

    Joined:
    Jan 27, 2006
    #8
    No less, that's a pretty decent display of the flow of information.
     

Share This Page