Prime numbers. Write a program that prompts the user for an integer and then pri
ID: 668616 • Letter: P
Question
Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print:
2
3
5
7
11
13
17
19
Recall that a number is a prime number if 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, constructs a PrimeGenerator object, and prints the primes.
____________________
/** This class returns all the prime numbers,
one at a time starting with 2.
This class follows the "generator" pattern: each
call to nextPrime() generates the next number in
the series.
@author Your Name
*/
public class PrimeGenerator
{
/**
Initialize the PrimeGenerator object with the
first number.
*/
public PrimeGenerator()
{
current = 1;
}
/**
Calculate the next prime number after current.
@return the next prime number
*/
public int nextPrime()
{
current++; // Increment past previous prime number.
// Write a loop that uses the value of the variable current,
// and while isPrime(current) is false, increment current.
// (When the loop exits, current contains the next prime number,
// which we can return.)
return current;
}
/**
Determine whether the given number is prime.
@param n Number to check
@return true if prime, or false if not
*/
public static boolean isPrime(int n)
{
// Write "if" comparisons and a loop to determine whether
// "n" is prime. We'll work this method out in class.
***
import java.util.Scanner;
/**
Print all the prime numbers less than or equal to the number
entered by the user.
@author Your Name
*/
public class PrimePrinter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
PrimeGenerator generator = new PrimeGenerator();
System.out.print("Enter an integer: ");
int maxInt = in.nextInt();
// "maxInt" contains the number entered by the user.
// Make a PrimeGenerator object, and write a loop
// that uses the PrimeGenerator's nextPrime() method to
// get primes. If the prime is less than or equal to "maxInt",
// print it; otherwise, terminate the loop.
}
}
}
int current;
}
Explanation / Answer
// Program using Netbeans IDE
package primeprinter;
import java.util.Scanner;
public class PrimePrinter {
int current;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the range: ");
int maxInt = in.nextInt();
// "maxInt" contains the number entered by the user.
// Make a PrimeGenerator object, and write a loop
// that uses the PrimeGenerator's nextPrime() method to
// get primes. If the prime is less than or equal to "maxInt",
// print it; otherwise, terminate the loop.
PrimeGenerator generator = new PrimeGenerator();
int i=2;
int prime;
System.out.println("Prime numbers are:");
while(i<=maxInt){
if(i==2)
System.out.println(i+" ");
else{
prime=generator.nextPrime();
if(prime<=maxInt)
System.out.println(prime+" ");
}
i++;
}
}
}
/** This class returns all the prime numbers,
one at a time starting with 2.
This class follows the "generator" pattern: each
call to nextPrime() generates the next number in
the series.
*/
class PrimeGenerator
{
int current;
/**
Initialize the PrimeGenerator object with the first number.*/
public PrimeGenerator()
{
current = 1;
}
/**
Calculate the next prime number after current.
@return the next prime number */
public int nextPrime()
{
current++; // Increment past previous prime number.
// Write a loop that uses the value of the variable current,
// and while isPrime(current) is false, increment current.
// (When the loop exits, current contains the next prime number,
// which we can return.)
while(!isPrime(current))
{
current++;
}
return current;
}
/**
Determine whether the given number is prime.
@param n Number to check
@return true if prime, or false if not
*/
public static boolean isPrime(int n)
{
// Write "if" comparisons and a loop to determine whether
// "n" is prime. We'll work this method out in class.
boolean flag=false;
int count=1;
for(int j=3;j<=n;j++)
{
if(n%j==0)
count++;
}
if(count==2)
flag=true;
return flag;
}
}
//output
run:
Enter the range: 20
Prime numbers are:
2
3
4
5
7
11
13
17
19
BUILD SUCCESSFUL (total time: 3 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.