Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am desperately in need of help writing a java program that will generate prime numbers This is my main method that will print my

I am desperately in need of help writing a java program that will generate prime numbers

This is my main method that will print my output ->

package primegeneratordemo;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class PrimeGeneratorDemo

{

public static void main(String[] args)

{

Scanner cin = new Scanner(System.in);

System.out.print("Enter a positive integer -> ");

int n = cin.nextInt();

PrimeGenerator primeSeq = new PrimeGenerator(n,'E');

System.out.printf("Is %d a prime number? %b%n",n,primeSeq.isPrime(n));

System.out.printf("Prime numbers in [1,%d] are %s.%n",n,primeSeq);

System.out.printf("The largest prime number in [1,%d] is %d.%n",n,primeSeq.getMax());

ArrayList primes = primeSeq.generate(n);

System.out.printf("The number of prime numbers in [1,%d] is %d.%n",n,primes.size());

//Add code to generate a random prime in [2,n] and the table shown on handout:

}

}

___________________________________________________________________________________________

This is the interface class that goes with the main ->

package primegeneratordemo;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class PrimeGeneratorDemo

{

public static void main(String[] args)

{

Scanner cin = new Scanner(System.in);

System.out.print("Enter a positive integer -> ");

int n = cin.nextInt();

PrimeGenerator primeSeq = new PrimeGenerator(n,'E');

System.out.printf("Is %d a prime number? %b%n",n,primeSeq.isPrime(n));

System.out.printf("Prime numbers in [1,%d] are %s.%n",n,primeSeq);

System.out.printf("The largest prime number in [1,%d] is %d.%n",n,primeSeq.getMax());

ArrayList primes = primeSeq.generate(n);

System.out.printf("The number of prime numbers in [1,%d] is %d.%n",n,primes.size());

//Add code to generate a random prime in [2,n] and the table shown on handout:

}

}

__________________________________________________________________________________

This is the class that I started and need help completeing to get my output ->

public class PrimeGenerator //implements PrimeGeneratorAPI { public PrimeGenerator() { } public PrimeGenerator(int n, char alg) {

} private void eratosthenesSieve ( boolean [ ] seq, int n ) { seq[0] = false; seq[1] = false; seq[2] = true; for (int k = 3; k

__________________________________________________________________________________

The class I need help completing requires these methods

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

The output I need to get will be like this example output

image text in transcribed

Here is a link to the full doc with all the needed info: https://drive.google.com/file/d/1nBSmXKjKavjhxxuJ19i4QKFIYhj4RX_D/view

The class will also contain two private auxiliary methods that fill the Boolean sequence using the Sieve of Eratosthenes and brute-force algorithm: Listing 6: Sieve of Eratosthenes An auxiliary method that sets seq [k] to true ifk is prime * and false f k is composite using the Eratosthenes Sieve * algorithm. * Oparam seq a boolean array that indicates whether or not * k is prime private void eratosthenesSieve (boolcan [seq) Listing 7: Sieve of Eratosthenes *An auxiliary mcthod that sets seq k] to truc if k is prime + and falsef k is composite using the brute-force algorithm * param seq a boolean array that indicates whether or not +k is prime private void naiveSieve (boolean [] seq)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago