PDA

View Full Version : need help modifying one method of a class in java




macman2790
Mar 3, 2007, 11:39 PM
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).


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:



Doctor Q
Mar 4, 2007, 12:55 AM
Is this for a school assignment, so we should give you tips but not finished code?

macman2790
Mar 4, 2007, 12:58 AM
Is this for a school assignment, so we should give you tips but not finished code?

yes i'm asking for tips. thanks, but anything will help

dcr
Mar 4, 2007, 01:10 AM
Visit a tutor or your instructor's office hours if you're having trouble.

http://www.cs.utsa.edu/~javalab/cs1713/instructors.html

macman2790
Mar 4, 2007, 01:17 AM
Visit a tutor or your instructor's office hours if you're having trouble.

http://www.cs.utsa.edu/~javalab/cs1713/instructors.html

um.... how do you know where i go to school? lol and better yet my class.

Eraserhead
Mar 4, 2007, 08:46 AM
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 (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html), you will need to store the numbers Integer (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html)'s instead of int's. (or Boolean (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Boolean.html)'s in this case)

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

dcr
Mar 4, 2007, 07:42 PM
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
Mar 4, 2007, 10:29 PM
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.