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

Create a Class containing a static method that can be used for future programs a

ID: 3571691 • Letter: C

Question

Create a Class containing a static method that can be used for future programs and as practice for creating future methods. The method must use the given Method header and must have java documentation. It should also include comments inside the method where appropriate.

1.) Determine If a Positive Integer is Prime Method Header: public static boolean isPrime(int number)

A positive integer is considered prime if its only be divided evenly by 1 and itself. This method should take in an integer and determine if it is a prime number. If the number is prime the method should return true otherwise, it should return false.

Hint: 2 is the smallest prime number, so any number below 2 is not a prime.

Explanation / Answer

CheckPrime.java

public class CheckPrime {
  
/* This method will check whether the number is prime or not
* Params :integer type number
* Return :boolean value.
*/
public static boolean isPrime(int number) {
   if(number<2)
       return false;
// If the user entered number is '2' return true
else if(number == 2)
return true;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}

}

____________________________

TestClass.java

import java.util.Scanner;

public class TestClass {

   public static void main(String[] args) {
      
   //Declaring variables
   int number;
   boolean bool;
  
   //Scanner class object is used to read the inputs entered by the user
   Scanner sc=new Scanner(System.in);
  
  
   /* This loop continues to execute until
   * the user enters a positive number
   */
   while(true)
   {
       //Getting the number entered by the user
   System.out.print("Enter a positive number :");
   number=sc.nextInt();
   if(number<0)
   {
       //Displaying the error message
       System.out.println("** Invalid.Number Must be Positive **");
       continue;
   }
   else
       break;
   }  
  
   //calling the method by passing the number as argument
       bool = CheckPrime.isPrime(number);
  
       /* If the returned 'bool' value is true
* Then display is prime message
* If not,display not a prime
*/
if (bool)
System.out.println(number+" is Prime");
else
   System.out.println(number+" is not Prime");

   }

}

____________________________

_______________________

Output:

Enter a positive number :1
1 is not Prime

_______________________

Output:

Enter a positive number :19
19 is Prime

_______________________

Output:

Enter a positive number :15
15 is not Prime

_______________________

Output:

Enter a positive number :-5
** Invalid.Number Must be Positive **
Enter a positive number :37
37 is Prime

______Thank You