I am desperately in need of help writing a java program that will generate prime
ID: 3742422 • Letter: I
Question
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<Integer> 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<Integer> 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 < n; k++)
seq[k] = k % 2 != 0;
for (int i = 3; i < Math.sqrt(n); i+=2)
if (seq[i] = true){
for (int j = i*i; j < n; j+=2*i)
seq[j] = false;
}
}
private void naiveSieve ( boolean [ ] seq )
{
}
}
__________________________________________________________________________________
The class I need help completing requires these methods
The output I need to get will be like this example output
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)Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; // collection package is imported as array is used
import java.util.List; // list is a part of collection package
import java.util.Random;
import java.util.Scanner; // this is imported for user input
class Main // Main Class
{
public static void play(List<Sequence> list, Scanner scan) {
Random rand = new Random(); // Object is created for random class
int n = rand.nextInt(4) + 1; // user input of int data type
Sequence seq = list.get(n);
// print statement for the input of next number in the series.
System.out.print("What is the next number in this sequence? ");
Integer nums[] = seq.getSequence();// to retrieve the sequence
for (int i = 0; i < 6; i++) // for loop is defined
{
System.out.print(nums[i] + ", "); // print statement to print the
// numbers
}
System.out.println("..."); // print the ...
List<Integer> randomNumbers = getRandomNumbers(nums);
if (!randomNumbers.contains(nums[6])) // for putting the correct answer
// in choice in //the case not yet
// present
{
int index = rand.nextInt(3) + 1; // user input of int data type.
randomNumbers.set(index, nums[6]);
}
// to the options to the users
for (int i = 0; i < 4; i++) // for loop is defined
{
System.out.println(randomNumbers.get(i)); // print the no.s
}
// id defined
// For getting user's choice
System.out.print("Enter the number: ");// print statement to enter the
// numbers.
int num = scan.nextInt();// For the user input
// if conditional statement is defined in order to check if the chosen
// number is correct or //not
if (nums[6] == num)
System.out.println("Your choice was correct");// print if chosen
// number is right
else
System.out.println("Your choice was wrong");// print if the chosen
// number is not //right
}
public static void main(String[] args) // Main method
{
List<Sequence> list = new ArrayList<Sequence>();// list is created for
// storage of array
// intance of classe are put in the classified list
list.add(new Fibonnaci()); // for fibonacci series
list.add(new MultipleOf3());// for the factors of 3
list.add(new MultipleOf5());// for multiple of 5
list.add(new MultipleOf8());// for the factors of 8
list.add(new MultipleOf10());// for the factors of 10
Scanner scan = new Scanner(System.in);
while(true)
{
play(list, scan);
scan.nextLine();
System.out.print("Do you wish to quit? (Y/N): ");
String choice = scan.nextLine();
if (choice.equalsIgnoreCase("y"))
{
break;
}
}
scan.close();
}
public static List<Integer> getRandomNumbers(Integer arr[])// in order to
// get the random
// //numbers fron
// the sequence
// array
{
Integer[] arr1 = new Integer[10];// object is created
for (int i = 0; i < 10; i++)
// for loop
arr1[i] = arr[i]; // defining the array
List<Integer> list = Arrays.asList(arr1); // array list is defined
Collections.shuffle(list);
return list.subList(0, 4); // shows the return
}
}
MultipleOf3.java
public class MultipleOf3 implements Sequence // again class MultipleOf3
// implements
// interface Sequence //through
// inheritance
{
Integer[] arr = new Integer[10]; // object creation
public MultipleOf3() // default constructor is defined
{
table(1, 3); // recursion is used for the table of 3
}
public Integer[] getSequence() // getSequence method
{
return arr;
}
// Variables are defined
public int table(int i, int n) {
int num;
// if else conditional statement
if (i == 11)
{
return 0; // returns zero
} else {
// calculation for the table
num = n * i;
arr[i - 1] = num;
return (table(++i, n)); // returns the result as table
}
}
}
MultipleOf10.java
public class MultipleOf10 implements Sequence // again class MultipleOf10
// implements
// interface Sequence //through
// inheritance
{
Integer[] arr = new Integer[10]; // object creation
public MultipleOf10() // default constructor is defined
{
table(1, 10); // recursion is used for the table of 10
}
public Integer[] getSequence() // getSequence method
{
return arr;
}
// Variables are defined
public int table(int i, int n) {
int num;
// if else conditional statement
if (i == 11)
{
return 0; // returns zero
} else {
// calculation for the table
num = n * i;
arr[i - 1] = num;
return (table(++i, n)); // returns the result as table
}
}
}
Rest of the classes/interface are same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.