Write a Java program that meets the following requirements: Declare a method to
ID: 3618412 • Letter: W
Question
Write a Java program that meets the following requirements:
Declare a method to determine whether an integer is a primenumber
Use the following method declarations: public static BooleanisPrime (int num)
An integer greater than 1 is a prime number if its onlydivisor is 1 or itself. For example, isPrime (11) returnstrue, and isPrime (9) returns false.
Us the isPrime method to find the first thousand prime numbersand 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 8389 97 … …
Important Notes: The input and output must use JOptionPanedialog and display boxes.
//Assignment Name: -Assignment5.1PrimeNumber.java
// PrimeNumber.java: Determine whether an integer is a primenumber
import javax.swing.JOptionPane;
public class PrimeNumber {
public static void main(String[] args) {
String numStr;
do{
numStr = JOptionPane.showInputDialog(null, "Enter a number between1 and 1000");
if(numStr==null || numStr.trim().equals("")){
continue;
}
if("0".equals(numStr)){
break;
}
int input = Integer.parseInt(numStr);
if(input>0){
if(isPrime(input)){
JOptionPane.showMessageDialog(null, input + " is prime");
}else{
JOptionPane.showMessageDialog(null, input + " is not a prime");
}
}else{
JOptionPane.showMessageDialog(null, "Enter number >0");
}
}while(true);
} // end main()
public static boolean isPrime ( int num )
{
boolean prime = true;
int limit = (int) Math.sqrt ( num );
for ( int i = 2; i <=limit; i++ )
{
if ( num % i == 0 )
{
prime = false;
break;
}
}
return prime;
}
}
Declare a method to determine whether an integer is a primenumber
Use the following method declarations: public static BooleanisPrime (int num)
Explanation / Answer
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testjavaconsoleaps; import javax.swing.JOptionPane; /** * */ public class PrimeNumber { public static void main(String[] args) { String numStr; String option =JOptionPane.showInputDialog(null, "Select an Option for displayPrime Numbers. 1 --> for printing Primenumbers between 1- 1000. 2 --> for checking whether the given number isprime or not"); if(option.equals("1")) { StringBuffer objBuffer= new StringBuffer(); int rowcnt = 0; for(int i = 3;i=10) { rowcnt = 0; objBuffer.append(" "); } } } JOptionPane.showMessageDialog(null,objBuffer.toString()); } else { do { numStr = JOptionPane.showInputDialog(null, "Enter a number between1 and 1000. (Enter 0 to exit)"); if(numStr==null || numStr.trim().equals("")) { continue; } if("0".equals(numStr)) { break; } int input = Integer.parseInt(numStr); if(input>2) { if(isPrime(input)) { JOptionPane.showMessageDialog(null, input + " is prime"); } else { JOptionPane.showMessageDialog(null, input + " is not a prime"); } } else if(input == 2) { JOptionPane.showMessageDialog(null, input + " is prime"); } else { JOptionPane.showMessageDialog(null, "Enter number >0"); } } while(true); } } public static boolean isPrime(int num) { int nLimit = (int)Math.sqrt((double)num); for(inti=2;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.