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

Using the attached template PrimeApp.java, write a method isPrime that should re

ID: 3640932 • Letter: U

Question

Using the attached template PrimeApp.java, write a method isPrime that should
receive an integer number and return true if the number is prime and false
otherwise. Incorporate this method into an application that asks the user to enter a
number, then calls the method isPrime to determine if the number is prime or
not. (A prime number is only divisible by 1 and itself).
Note: You must use the methods of the JOptionPane class to interact with the
user.

This is the date from the file

//PrimeApp.javaimport javax.swing.JOptionPane;
public class PrimeApp{      /* write code of the isPrime method */


      public static void main(String [] args)      {       

 PrimeApp application = new PrimeApp();

         /* write code to obtain user input from         

          JOptionPane input dialogs */

        /*write code here to call the isPrime method and  to output whether the number is Prime or Not prime using   JOptionPane message dialogs*/

      }
 } // end class PrimeApp

Explanation / Answer

import javax.swing.JOptionPane;

public class PrimeApp {

   public static void main(String [] args) {
       int n = 0;
      
       n = Integer.parseInt(JOptionPane.showInputDialog("Please enter a natural number: "));

       JOptionPane.showMessageDialog(null, n + (isPrime(n) ? " is " : " is NOT ") + "a prime number!" );
   }
  
   private static boolean isPrime(int n) {
       if (n == 2) {
           return true;
       }
       else if (n < 2 || n % 2 == 0) {
           return false;
       }
       else {
           for (int divisor = 3; divisor <= Math.sqrt(n); divisor += 2) {
               if (n % divisor == 0) {
                   return false;
               }
           }
           return true;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote