Java Prime Number


1.PrimeFinder.java
import java.util.ArrayList;
import java.util.List;

public class PrimeFinder implements Runnable {

	private Integer start; 
	private Integer end; 
	private List primes;
	
	PrimeFinder(Integer startNum, Integer endNum) { // Constructs a PrimeFinder 
		this.start = startNum;
		this.end = endNum;
	}

	public List getPrimesList() { // Returns the values of the attribute primes  
		return this.primes;		
	}

	public boolean isPrime(int n) { // Determines whether its argument is prime or not 
		for (int i=2;i();
		this.primes.clear();
		
		for (int i=this.start; i <= this.end; i++){
			//System.out.print("Start: " + this.start);
			//System.out.print("End: " + this.end);
			//System.out.print("Processing Number: " + i);
			if (this.isPrime(i) && i>1) this.primes.add(i);
		}

		AssignmentOne.printPrime.addAll(this.primes);
	}
	
	
}

2.AssignmentOne.java
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class AssignmentOne {
	public static List printPrime;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//List all of the numbers
		System.out.print("List of number as: ");
		for (int i=0; i intervalList = new ArrayList();
		for (int i=0; i< args.length-1; i++){
			Integer[] myInteval = new Integer[]{Integer.parseInt(args[i]), Integer.parseInt(args[i+1])-1};
			intervalList.add(myInteval);
		}

		//Call to get the list of primes
		new AssignmentOne().lprimes(intervalList);

		//Print out the results
		if (AssignmentOne.printPrime.size() > 0) {
			System.out.print("\nList Prine: [");
			for (int i=0; i lprimes(List intervals) {// Return the list of primes from helper class

		AssignmentOne.printPrime = new ArrayList();
		AssignmentOne.printPrime.clear();

		if (intervals.size() > 0){
			for (int i=0; i < intervals.size(); i++){
				Integer[] actionInterval = intervals.get(i);
				PrimeFinder primeObject = new PrimeFinder(actionInterval[0], actionInterval[actionInterval.length-1]);

				Thread t = new Thread(primeObject);
				t.setName("This is Thread " + i);
				t.setDaemon(true);
				t.start();
				
				if (t.isAlive()) {
					try {
						//System.out.println("\nThread " +t.getName()+ " are still running. Pls wait ........");
						Thread.sleep(3*1000);
					}
					catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}

		return AssignmentOne.printPrime;
	}


}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.