Write a Java program that meets the following requirements: Declare a method to
ID: 3618681 • Letter: W
Question
Write a Java program that meets the following requirements:
Declare a method to determine whether an integer is a prime number
Use the following method declarations: public static Boolean isPrime (int num)
An integer greater than 1 is a prime number if its only divisor is 1 or itself. For example, isPrime (11) returns true, and isPrime (9) returns false.
Us the isPrime method to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 61 73 79 83 89 97 … …
//imports
import javax.swing.*;
//Main class starts
public class Main
{
// Main function starts
public static void main(String[] args){
//Declare variable
String numStr;
//do starts
do{
//prompts on the screen to user Enter a number between 1 and 1000 (0 to EXIT)
numStr = JOptionPane.showInputDialog(null, "Enter a number betwen 1 and 1000 {-1 to SHOW PRIME NOS. BETWEEN
1 and 1000 / 0 to EXIT)");
// if user enters a blank than it will again ask to enter a number
if(numStr==null || numStr.trim().equals(")){
continue;
}
//If user enters 0 exits the program
if("0".equals(numStr)){
break;
}
//Parse the input number to integer type and assign it to a local variable
int input = Integer.parseInt(numStr);
//If input is greater than 0 it will check for prime or not prime
if(input>0){
//If the number is prime, it will display on the screen as number is prime
if(isPrime(input)){
JOptionPane.showMessageDialog(null, input + " is prime");
}
//If the number is not prime, it will display on the screen as number is not prime
else{
JOptionPane.showMessageDialog(null, input + " is not a prime");
}
else if (input == -1){
break;
}
//If the input is wrong it will prompt to enter again
else{
JOptionPane.showMessageDialog(null, "Enter number > 0");
}
}
// While user chooses not to quit the prompt will continue while(true);
// If userselects -1 prime numbers will get displayed
if("-1".equals(numStr)){
//Declare and initialize variables
String sOut = " ";
int iCounter = 0;
//Calculates and displays the prime numbers between 1 and 1000 in console
for(int iCount = 1 ; iCount < 1000 ; iCount ++){
boolean iPrime = isPrime(iCount);
if(iPrime == true){
//If the number is prime add to the string
sOut = sOut + " " + iCount;
//Increment iCounter and when it reaches a multiple of 10 gives a line break
iCounter++;
if(iCounter%10 == 0){
sOut = sOut + " ";
}
}
}
//Displays the uotput on the screen
System.out.print("---------------------------------------------- "
+ "Prime numbers from 1 to 1000 "
+ "---------------------------------------------- "
+ sOut
+
" ---------------------------------------------- ");
}
}
//Function isPrime to check if a number is prime or not
public stati
Declare a method to determine whether an integer is a prime number
Use the following method declarations: public static Boolean isPrime (int num)