PirmeDirective.java even methode

// Import statement:

import java.util.ArrayList;

class PrimeDirective {

// Add your methods here:

public Boolean isPrime(int number){

if (number == 2){   //7 != 2 skip

    return true;

  }else if(number < 2){ //7 < 2 skip

    return false;

  }

for(int n = 2; n < number; n++) {

   if(number % n == 0){

     return false;

    } else{

      return true;

    }

}

return false;

}

public Boolean isEven(int number){

if( number%2==0){

  return true;

}else{

  return false;

}

}

public ArrayList onlyEven(int numbers){

ArrayList<Integer> even = new ArrayList<Integer>();

for(Integer number : numbers){

  if(isEven(number)){

    even.add(number);

  }

}

return even;

}

public ArrayList onlyPrimes(int numbers){

ArrayList<Integer> primes = new ArrayList<Integer>();

for(Integer number : numbers){

  if(isPrime(number)){

    primes.add(number);

  }

}

return primes;

}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();

int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

  

    System.out.println(pd.isPrime(7));

    System.out.println(pd.isPrime(28));

    System.out.println(pd.isPrime(2));

    System.out.println(pd.isPrime(0)); 

    

    System.out.println(pd.onlyPrimes(numbers));

    System.out.println(pd.isEven(numbers));

    }  

}

compiler say :slight_smile:

$ javac PrimeDirective.java
^[[APrimeDirective.java:69: error: incompatible types: int cannot be converted to int
System.out.println(pd.isEven(numbers));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

but isn`t the same type as onlyPrimes ???

1 Like

Your method isEven() takes a parameter of type int and returns a boolean.

The paramater you tried to put in is of type array (int[]) so it tells you int[] cannot be converted to int.

By writing the isEven() method like this you are telling the machine you will only give it objects of type int.

Questions for you to consider:

  • how does one access a single element of an array to make this function work?
  • is there a way to make the function check every element of an array with a simple coding technique?
1 Like