www.webdeveloper.com
Results 1 to 12 of 12

Thread: Java program needed !!

  1. #1
    Join Date
    Oct 2004
    Posts
    9

    Java program needed !!

    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......

  2. #2
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Code:
    //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();
    		}
    	}
    }
    Code:
    //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);
    		}
    	}
    }

  3. #3
    Join Date
    Oct 2004
    Posts
    9
    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

  4. #4
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    When it asks for a file name just hit enter.

  5. #5
    Join Date
    Oct 2004
    Posts
    9
    one other thing, can I put it all in one file instead of two?

  6. #6
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    I would imagine it would be ok if you put the PrimeThread class in the Primes.java file.

  7. #7
    Join Date
    Oct 2004
    Posts
    9
    O.k....but I don't want it to ask for a file........

  8. #8
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    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!

  9. #9
    Join Date
    Oct 2004
    Posts
    9
    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

  10. #10
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    What's the purpose then?

  11. #11
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Multiple threads writing to the same unsynchronized output stream isn't a good idea anyway.

  12. #12
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    So wrap all the print statments in synchronized blocks... not hard to do.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles