In JAVA language, create PrimeGenerator class that implements PrimeGeneratorAPI.
ID: 3742305 • Letter: I
Question
In JAVA language, create PrimeGenerator class that implements PrimeGeneratorAPI.java, that outputs the following Sample Run:
There should be three java files: PrimeGeneratorDemo.java , PrimeGeneratorAPI.java , and PrimeGenerator.java .
PrimeGenerator class must have:
/
Creates a Boolean sequence of length 100 using the Sieve of
Eratosthenes , where sequence [ k ] i s true , when k
false when k is composite ./
public PrimeGenerator ()
/
Generates a boolean sequence [0 ,... , length ] that indicates whether sequence[k] is prime or composite using the
specified algorithm
@param n an integer >= 1
@param alg ’E’ or ’e’ for the Sieve of Eratosthenes sieve and ’n’ or ’N’ for the naive/bruteforce algorithm
@throws IllegalArgumentException when n < 1 or alg is not ’n’, ’N’, ’E’ or ’e’
/
public PrimeGenerator(int n,char alg) throws IllegalArgumentException
/
An auxiliary method that sets seq[k] to true if k is prime
and false if k is composite using the Eratosthenes Sieve
algorithm .
@param seq a boolean array that indicates whether or not
k is prime/
private void eratosthenesSieve(boolean [] seq)
/
An auxiliary method that sets seq[k] to true if k is prime
and false if k is composite using the bruteforce algorithm. @param seq a boolean array that indicates whether or not
k is prime
/
private void naiveSieve(boolean [] seq)
and include the isPrime algorithm, bruce-force algorithm, and sieve of eratosthenes algorithm (the following images are in pseudocode -> translate in JAVA):
PrimeGeneratorAPI must be implemented in PrimeGenerator class and the following methods must be extracted:
PrimeGeneratorAPI.java
The following Corollaries should help with getMax and getPrime functions:
After PrimeGenerator class is completed with all the above methods, PrimeGeneratorDemo will produce the sample run output above, complete the commented out part of the PrimeGeneratorDemo.java :
Finished run should look like Sample Run,
Sample Run: Enter a positive integer -> 50 Is 50 a prime number? false Prime numbers in [1,50] are [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] The largest prime number in [1,50] is 47 The number of prime numbers in [1,50] is 15. A random prime in [1,50] is 7. Empirical Analysis of Brute-force vs Sieve of Eratosthenes Prime Sequence Generation Algorithms Eratosthenes (ns) max Prime in [1,n] Naive(ns) max Prime in [1,n] 400000 800000 1200000 1600000 2000000 2400000 2800000 3200000 3600000 4000000 4400000 4800000 5200000 5600000 6000000 6400000 6800000 7200000 7600000 8000000Explanation / Answer
Answer:
public static int randomPrime(int lower, int higher) {
if (lower < 0)
throw new IllegalArgumentException("lower must be positive.");
if (lower >= higher)
throw new IllegalArgumentException("lower must be smaller the higher.");
if (!containsPrime(lower,higher))
throw new IllegalArgumentException("no Primes in this interval.");
if (rand == null) {
rand = new Random();
}
int out = rand.nextInt(higher - lower) + lower;
while (!isPrime(out)) {
out = rand.nextInt(higher - lower) + lower;
}
return out;
}
private static boolean containsPrime(int lower, int higher) {
if (isPrime(lower)||isPrime(higher-1)){
return true;
}
if (lower % 2 == 0) lower += 1;
else lower += 2;
while (lower<higher){
if (isPrime(lower)){
return true;
}
lower +=2;
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.