Click to See Complete Forum and Search --> : Java program needed !!
kickerman97
10-10-2004, 05:19 PM
If someone could help me, i would appreciate it. I am new to Java and need some help. I need to write a multithreaded java or pthreads program that outputs prime numbers. The user needs to run the program and enter a number on the command line. The program needs to create a seperate thread that outputs all the prime numbers less than or equal to the number that the user entered. Thanks in advance......
HaganeNoKokoro
10-10-2004, 06:26 PM
//Main Program Primes.java
import java.io.*;
public class Primes {
public static void main(String args[]) {
try {
PrimeThread pt=null;
//read in parameters
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number> ");
int limit=Integer.parseInt(br.readLine());
System.out.print("Enter a file name to store the results> ");
String fName=br.readLine();
//create PrimeThread
if(fName.length()>0) pt=new PrimeThread(limit, new FileOutputStream(fName)); //output to file
else pt=new PrimeThread(limit); //output to standard output stream
pt.run(); //run the thread
} catch(Exception e) { //implement better exception handling than this
e.printStackTrace();
}
}
}
//Thread class PrimeThread.java
import java.io.*;
class PrimeThread extends Thread {
private PrintStream pOut=null;
private int limit=0;
//default constructor. does nothing
public PrimeThread() {
}
//constructor to set the number below which to generate primes
//no output stream is specified, so it outputs to the System.out
public PrimeThread(int l) {
limit=l;
try {
pOut=System.out;
} catch(Exception e) {
e.printStackTrace();
}
}
//constructor that sets both the number, as above, and specifies an output stream
//if the specified stream is null, uses System.out
public PrimeThread(int l, OutputStream outS) {
limit=l;
try {
if(outS!=null) {
pOut=new PrintStream(outS);
} else {
pOut=System.out;
}
} catch(Exception e) {
e.printStackTrace();
}
}
//method that performs the work of the thread, in this case the generation of prime numbers.
public void run() {
//compute primes via the seive
boolean numbers[]=new boolean[limit+1];
numbers[0]=false; //need to set 0 not prime
numbers[1]=false; //need to set 1 not prime
for(int i=2; i<numbers.length; i++) { //initially set all other numbers potentially prime
numbers[i]=true;
}
for(int i=2; i<numbers.length; i++) {
if(numbers[i]) { //if this number has been eliminated, so have its multiples
for(int j=(2*i); j<numbers.length; j+=i) { //eliminate multiples of the current number
numbers[j]=false;
}
}
}
for(int i=0; i<numbers.length; i++) { //output the numbers found to be prime
if(numbers[i]) pOut.println(i);
}
}
}
kickerman97
10-10-2004, 09:29 PM
thanks alot. Can you show me what to change to make it display the reults on the screen instead of asking for a file to save them in? Thanks again
HaganeNoKokoro
10-10-2004, 09:42 PM
When it asks for a file name just hit enter.
kickerman97
10-10-2004, 09:45 PM
one other thing, can I put it all in one file instead of two?
HaganeNoKokoro
10-10-2004, 09:51 PM
I would imagine it would be ok if you put the PrimeThread class in the Primes.java file.
kickerman97
10-10-2004, 09:59 PM
O.k....but I don't want it to ask for a file........
HaganeNoKokoro
10-10-2004, 10:17 PM
This sounds like a school assignment, and I've already done waaay too much.
There is another constructor for the PrimeThread class that will not require a file name, and it should be easy enough for you to look through the 25 or so lines of code in Primes.java and remove the part where it asks for a file name.
You can do it! I believe in you! Insert generic inspirational encouragement here!
kickerman97
10-10-2004, 10:36 PM
you don't have to help anymore if you don't want to, but I can assure you its not a school assignment.
by-the-way....thanks alot
HaganeNoKokoro
10-10-2004, 10:52 PM
What's the purpose then?
ray326
10-10-2004, 10:55 PM
Multiple threads writing to the same unsynchronized output stream isn't a good idea anyway.
HaganeNoKokoro
10-10-2004, 11:02 PM
So wrap all the print statments in synchronized blocks... not hard to do.