Https://www.codecademy.com/courses/learn-java/projects/java-prime-directive

https://www.codecademy.com/courses/learn-java/projects/java-prime-directive

My code doesn’t work

// Import statement:
import java.util.ArrayList;

class PrimeDirective {
  
  // Add your methods here
  public boolean isPrime(int number){
    boolean primeAnswer = true;
  
    if (number < 2) {
      primeAnswer = false;
      return primeAnswer;
    }
    for (int i = 2; i < number; i++){
        if (number % i == 0) {
          primeAnswer = false;
          return primeAnswer;          
        }
    }   
    return primeAnswer;
  }
  
  
  public ArrayList<Integer> onlyPrimes(int[] numbers) {
   //Declaration of Array
    ArrayList<Integer> primes = new ArrayList<Integer>(); 
  //add to Array List if True
    for (int number : numbers) {
      if (isPrime(number)) {
        primes.add(number);
      }
    }
    return primes;
  }
  
  // MAIN METHOD
  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();
    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89, 1, 31, 17};

    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));
  }
}  

You have public methods, but no public class as the error message states.

1 Like