2 Replies - 1337 Views - Last Post: 23 February 2012 - 11:07 AM Rate Topic: -----

#1 samuel17  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 209
  • Joined: 14-October 10

Java Prime Number Finder 1-100

Posted 22 February 2012 - 05:00 PM

Hi All,
I have an assignment ive been working on that i cant quite get. I have 95% of it done. I emailed my instructor and he gave some assistance, basically telling me where im missing code. The
public void setElement(int x)
is the most obvious part. I have searched through my book and online but couldnt find anything to help. The assignment is using threads so i needed to incorporate that into the project, making it even funner :P It is suppose to be set to a standard 0-100, no user input. Could possibly just use a mental break :P Any way, for the code:
import java.io.*;
import java.util.Arrays;
/** 
* Discovery project 3
**/
public class Discovery5_3 extends Thread {
	
	private boolean holder[] = new boolean[100];
	private int prime = 2;
	
	public void run() {
		findPrime(prime);
		prime +=1;
		holder[prime] = true;
	}
	
	public void setarray() {
		Arrays.fill(holder,true);
	}
	public void setElement(int x) {
		//Add Code Here
	}
	public String getElements() {
		String y = new String();
		for (int x = 0; x < holder.length; x++) {
			if (this.holder[x] == true) {
				y += Integer.toString(x);//Is this right?
				if (x < 100)
					y += " , ";
			}
		}
		return y;
	} 
	public void findPrime(int x) {
		if (this.holder[x-1] == true) {
				for (int y = 2; y <= 50; y++) {
					if (x*y <= 100) {
					int z = (x*y) -1;
					this.setElement(z);
					}
			}
		}
	}
	public int primeValue() {
		return prime;
	}
	public static void main(String args[]) {
		Discovery5_3 x = new Discovery5_3();
		x.setarray();
		//Insert missing code here.
			Thread t = new Thread (x);
			t.start();
                        System.out.println("The following numbers are prime numbers:");
		System.out.println(x.getElements());
		}
		
	}
	



In this part:
public String getElements() {
		String y = new String();
		for (int x = 0; x < holder.length; x++) {
			if (this.holder[x] == true) {
				y += Integer.toString(x);//Is this right?
				if (x < 100)
					y += " , ";
			}
		}
		return y;
	} 

I added:
y += Integer.toString(x);//Is this right?

but all that does is print every single number now so im guessing that wasnt the correct thing to add. Could anyone help me with this and preferably with an explanation? I dont just want a hand out :)

Thank you all!
Sam

Is This A Good Question/Topic? 0
  • +

Replies To: Java Prime Number Finder 1-100

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,536
  • Joined: 05-May 05

Re: Java Prime Number Finder 1-100

Posted 22 February 2012 - 06:28 PM

Quote

but all that does is print every single number now so im guessing that wasnt the correct thing to add.


You're trying to print the index of every element that is true in holder, right? That's right of you to add that. However, if every number is printing, then your primes computation algorithm is wrong. By the looks of things, your algorithm looks overly complicated.
How about something like this:


private boolean holder[] = new boolean[100];
private int prime = 2;

public void run() {
    if (isPrime(prime))
        holder[prime] = true;
    prime++;
}

public boolean isPrime(int x) {
    int sqrtOfX = (int) Math.sqrt(x);
    for (int i = 2; i < sqrtOfX; i++)
        if (x % i == 0)
            return false;
    return true;
}



Get rid of setArray and leave your getElements method. Now you need to create multiple threads in main whose runnable object is the same instance of Discovery5_3. Note that this is dangerous as you can never guarantee the order in which threads will execute. You're assuming they'll execute in order, where each thread executes in full only after the previous one finishes. On my machine the output is correct despite this danger.

This post has been edited by blackcompe: 22 February 2012 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 samuel17  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 209
  • Joined: 14-October 10

Re: Java Prime Number Finder 1-100

Posted 23 February 2012 - 11:07 AM

hi blackcompe,
Thanks for the response. What is the point of the setArray? I seen an example in my book for it so i had copied that over. I guess i dont fully get the concept of the multithreading. from what i understand,one thread can stop and the other can continue going. So if one thread stops for user input, the other can keep working on calculations? Read all of that in my book :P it just doesnt quite make sense to me. The way i have it in the code seems so simplistic but i dont understand what its doing. Ok, i started a thread, what is that thread doing? I really appreciate the help, Java has never been my strong suit. I will have to read up again on this stuff when i get home and see if i cant make heads or tails out of it. Also, what about the setElement? Ive been piecing together code but the book is kind of vague on what it was setting. One week isnt enough to try to get it all done! haha

Sam

This post has been edited by samuel17: 23 February 2012 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1