简体   繁体   中英

prime numbers test in java

I have an assignment that tells me to do the following:

Create a method named IsPrime(), which accepts a positive integer as a parameter. If a number if prime, the method should return true. (A prime number is evenly divisible only by 1 and itself). Incorporate this method into a class named MyMathMethods. Create a main() method in a separate class called MainFile, which will test IsPrime() by asking the user for a number using an input dialog and will then report whether that number is prime or not.

How do I connect these two files? here is my code:

     package x;
     import java.util.Scanner;
    public class MyMathMethod
 { 
 public static boolean isPrime(int num)
 {


 int result=0;
   System.out.println("enter no");
  Scanner s = new Scanner(System.in);
  num =s.nextInt();
  long sqrt = (int) Math.sqrt(num);
  for(long divisor = 2; divisor <= sqrt; divisor++) 
 {
        if(num % divisor == 0)
{  
                // This only needs to happen once
              // for this number to NOT be prime
  return false;
        }
}
// If we get here, the number is prime.
return true;
  }
   }

Another File is

   import x.*;
   package y;
 public class MainFile {
  public static void main(String [] args) {

  boolean isNumberPrime;
    int num;
 MyMathMethod methods = new MyMathMethod();
 isNumberPrime = methods.isPrime(num);
{

if(num=true) System.out.println(num + " is Prime Number"); else System.out.println(num + " is not Prime Number"); } } }

Thanks in advance.

Do you want to call the method isPrime() from your main in another class?

When they're a in the same package, you have to create a new instance of your class MyMathMethods and call the method isPrime(). when they're in different class, you must import the missing class and do the same as above. If you don't import it manually, probably your IDE will do it for you or ask for it.

the first file:

package x;
public class MyMathMethods {
  public boolean isPrime(int number) {
    //your code here
    return false;
  }
 }

the second:

package y;
import x.* //importing all files from the package x
           //if your class MyMathMethods is in the same package, you don't need that
public class MainFile {
  public static void main(String [] args) {
    int number = 20; 
    boolean isNumberPrime;
    MyMathMethods methods = new MyMathMethods();
    isNumberPrime = methods.isPrime(number); //here the result if it's prime
  }
 }

EDIT: I'll try to fix your code.

If you want to have your method static, you can call it:

MyMathMethods.isPrime(numer);

without the need to create a new instace.

The next issue: why are you passing to the funtion a variable that is not initialized (num) and try to get this value from the input in this method? Well, that's not a good practise. I would rather suggest to get the input from the user in main (directly in main on in another function called up in main) and then pass the value to isPrime().

package y;
import x.* 
public class MainFile {
  public static void main(String [] args) {
    int num;
    Scanner s = new Scanner(System.in);
    num =s.nextInt();
    if(MyMathMethods.isPrime(num)) {
      //if true, your code;
    } else {
      //false, your code;
    }
  }
 }

 package x;
 public class MyMathMethod { 
 public static boolean isPrime(int num) {
   if (num <= 1) {           
     return false;
   }
   for (int i = 2; i < Math.sqrt(num); i++) {
     if (num % i == 0) {
       return false;
     }
   }
   return true; 
 }
}

Your algoritm failed for many numbers, eg 0, 1, 2. Well, for this values it doesn't even returned any value. Please always protect your method against any possible wrong parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM