- The method generatePrimes places the first set of primes in an array. Add the statements to the morePrimes method that fill the remaining locations in the array with the next primes. For each value, divide the value by all of the primes in the array; if the value is not evenly divisible by one of the primes, then that value is a prime and is to be added to the array in the next available location. Do not use any temporary arrays. This question does not require the use of the Sieve of Eratosthenes. The method is to terminate when all of the locations in the array are filled with prime values. Print the values in the array to System.out when the loop terminates.
- import java.util.Arrays;
- public class primeNumber
- {
- public static void main(String[] parms)
- {
- process();
- System.out.println("\nProgram completed normally.");
- }
- public static void process()
- {
- int[] primes;
- int numPrimes;
- primes = new int[25];
- numPrimes = generatePrimes(primes);
- System.out.println(Arrays.toString(primes));
- morePrimes(primes, numPrimes);
- System.out.println(Arrays.toString(primes));
- }
- public static int generatePrimes(int[] primes)
- {
- int[] initialPrimes = new int[]{2, 3, 5, 7, 11, 13, 17};
- int count;
- System.arraycopy(initialPrimes,0,primes,0,initialPrimes.length);
- return initialPrimes.length;
- }
- public static void morePrimes(int[] primes, int numPrimes)
- {
- int count;
- int value;
- boolean done;
- }
- }
复制代码 |
|