Russell Culpepper
Russell Culpepper

Reputation: 91

Design a class that tells whether a number is prime or not

My homework is to Design a class named MyInteger with the following conditions:

My problems come up in the static boolean isPrime method, stating that "/" and "%" are undefined for arguement type and in the main method in my if statement: isPrime() == true. It says to change it to static, but i already have a static boolean isPrime method and I'm supposed to have two isPrime methods according to my conditions. Thank you if you are able to help.

public class MyInteger {


    public MyInteger(int value){

    }
    public static int getValue(){
        int value = 997;
        return value;
    }
    public boolean isPrime(){
        int value = 997;
        for (int i=2; i<=value/2; i++){
            if(value % i == 0) {
                return false;
                }
        }
        return true;
    }
    public static boolean isPrime(MyInteger value){
        for(int i=2; i<=value/2; i++){
            if(value%i == 0){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
            MyInteger value = new MyInteger(MyInteger.getValue());
            if (isPrime()==true && isPrime(value)==true){
                System.out.println("Testiwng Instance method, is Prime");
                System.out.println("isPrime: " + value + " is prime");
                System.out.println("--------------------------------");
                System.out.println("Testing Class method (That takes a reference variable) is Prime");
                System.out.println("isPrime: " + value + " is prime");
            }
            else{
                System.out.println("Testiwng Instance method, is Prime");
                System.out.println("isPrime: " + value + " is not prime");
                System.out.println("--------------------------------");
                System.out.println("Testing Class method (That takes a reference variable) is Prime");
                System.out.println("isPrime: " + value + " is not prime");
            }

      } 
}

Upvotes: 0

Views: 2212

Answers (5)

Thilina Dharmasena
Thilina Dharmasena

Reputation: 2341

Another way to check the prime number

 public String isPrime(int number) {
    if (number < 0) {
        return "not a valid number";
    }
    if (number == 0 || number == 1) {
        return "not a prime number";
    }
    if (number == 2 || number == 3) {
        return "prime number";
    }
    if ((number * number - 1) % 24 == 0) {
        return "prime number";
    } else {
        return "not a prime number";
    }
}

Upvotes: 0

Shar1er80
Shar1er80

Reputation: 9041

Here's a good reference for testing prime numbers What would be the fastest method to test for primality in Java?

Primarily, change your isPrime() to

boolean isPrime(long n) {
    if(n < 2) return false;
    if(n == 2 || n == 3) return true;
    if(n%2 == 0 || n%3 == 0) return false;
    long sqrtN = (long)Math.sqrt(n)+1;
    for(long i = 6L; i <= sqrtN; i += 6) {
        if(n%(i-1) == 0 || n%(i+1) == 0) return false;
    }
    return true;
}

Upvotes: 2

Ivaylo Marinovski
Ivaylo Marinovski

Reputation: 143

You don't have to go till the half of the number to check whether it is prime. You can have a loop that checks only the numbers from 2 to the square root of your number. See this - StackOverflow question about checking prime numbers

I believe you need something like this:

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner inp = new Scanner(System.in);
        int someValue = inp.nextInt();
        MyInteger myInt = new MyInteger(someValue);
        System.out.println("Testing instance method:");
        System.out.println(myInt.isPrime());
        System.out.println("Testing static method:");
        System.out.println(MyInteger.isPrime(myInt));
    }
}

class MyInteger {
    private int value;

    public MyInteger(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public boolean isPrime() {
        int sqrt = (int) Math.sqrt((double)value);
        for(int i = 2; i <= sqrt; i++) {
            if (value % i == 0) return false;
        }
        return true;
    }

    public static boolean isPrime(MyInteger myInt) {
        return myInt.isPrime();
    }
}

Upvotes: 3

Sai G
Sai G

Reputation: 146

You should consider using class level variable to make use of constructor to initialize it. Also in main() method you are trying to access non-static method (isPrime()), use it as value.isPrime().

In case you don't want to use class variable, make use of static getValue() method inside your static boolean isPrime(MyInteger value) method which solves your problem

Upvotes: 1

Johannes Larsson
Johannes Larsson

Reputation: 11

Your value variable in the method you mentioned is of the type MyInteger, but you're trying to use it as an int. you probably want to use value.getValue() instead.

Upvotes: 1

Related Questions