Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need this program in java Assignment 1 Write a program that prompts the user f

ID: 3883191 • Letter: I

Question


i need this program in java

Assignment 1 Write a program that prompts the user for a positive integer and then prints out all prime numbers up to and including that integer. For example: if the user enters 20, the program should print. 5 13 17 19 Recall that if a number is a prime number it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method reads a user input from the command line using the Scanner and constructs a PrimeGenerator object and prints the primes. Follow all directions in the COP3337 Class Rules for Submitting Programs. Please use the file > export > to zip option in NetBeans. The zip file should be named FirstNameLastNameA1.zip.

Explanation / Answer

PrimeGenerator.java

public class PrimeGenerator {
//Declaring instance variable
private int num;

//Parameterized constructor
public PrimeGenerator(int num) {
this.num = num;
}

//This method will display the prime numbers
public void nextPrime() {
for (int i = 2; i <= num; i++) {
boolean bool = isPrime(i);
if (bool) {
System.out.println(i);
}

}
}

//This method will check whether the number is prime or not
private boolean isPrime(int n) {
// If the user entered number is '2' return true
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}

}

___________________

PrimePrinter.java

import java.util.Scanner;

public class PrimePrinter {

public static void main(String[] args) {

// declaring variable
int num;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter the number :");
num = sc.nextInt();

// Creating an PrimeGenerator class Object by passing the user entered
// input as arguemnt
PrimeGenerator pg = new PrimeGenerator(num);

// calling the method on the PrimeGenerator class
pg.nextPrime();
}

}

______________

Output:

Enter the number :20
2
3
5
7
11
13
17
19

_____________Could you rate me well.Plz .Thank You