Prime directive

https://www.codecademy.com/courses/learn-java/projects/java-prime-directive
Hi,
Having trouble with the exercise above.
My code so far looks like this, and I have been able to proceed up to step 8, which asks me to test run the program. However, there would always show an error in compiling that says I am missing a return statement

import java.util.ArrayList;
class PrimeDirective {
  
  // Add your methods here:
  public boolean isPrime(int number) {
    if (number == 2) {
      return true;
    } else if (number < 2) {
      return false;
    } else {
      for (int i = 2; i < number; i++) {
        if (number % i != 0) {
          return true;
        } else {
          return false;
        }
      }
    }
  }

Any help will be appreciated :slight_smile:

I am not an expert on Java, but…

I can only assume that Java is looking at your code, seeing that all your return statements are inside of an if/else structure and is throwing that error out of concern that your isPrime() method ought to be returning a boolean but doesn’t necessarily have a reachable return statement.

In short, you need a return outside of the if/else structure (I think)…

Thanks for your help! on to next steps :slight_smile:

1 Like

Hi,
https://www.codecademy.com/courses/learn-java/projects/java-prime-directive
Was wondering if anyone has gone through the same problem as mine or anyone who could help out (i know this is my third time posting on this topic…sorry…)
The program is running no problem, but I am wondering why the method isPrime is returning ‘true’ for every number when it should be only returning ‘true’ for prime numbers.
Any help will be greatly appreciated :slight_smile:

import java.util.ArrayList;
class PrimeDirective {
  boolean Primez;
  public boolean isPrime(int number) {
    if (number == 2) {
      Primez = true;
    } else if (number < 2) {
        Primez = false;
    } else {
        for (int i = 2; i < number; i++) {
          if (number % i != 0) {
          Primez = true;
        }   else {
          Primez = false;
        }
      }
    }
  return Primez;}
  
  public ArrayList<Integer> onlyPrimes(int[] numbers) {
    ArrayList<Integer> primes = new ArrayList<Integer>(); 
    for (int number : numbers) {
      if (isPrime(number) == true) {
        primes.add(number);
      }
      else {
        continue;
      }
    }
  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(6));

  }  

}

That might be the overall result but what happened to get there? Time to start observing the individual operations you’re carrying out?

A car mechanic doesn’t stop at “oh no this car is faulty” they pop open the hood and look at the individual parts.

btw comparing a boolean to true seems rather redundant. you can add comparisons like that all day and the result will still be the same as the value itself, so use that

if (a == true == true == true == true == true == true)
if (a)

similarly, having an else clause which does the same thing as would happen otherwise … just remove it.

As for your algorithm, consider how you would manually test whether a number is prime? Would you be toggling your answer back and forth a whole lot? Because if not, then your program shouldn’t be doing that either. It would seem like you’ve lost track of what is supposed to happen.

A start could be to create an array containing all the numbers you want to try dividing by.

Actually, you don’t really need an array, since you can test a value and then discard it, but it can be useful for seeing that you’re doing the right thing.

Then you might print that array, are those the right numbers to be using?

Then you might make another array with the results, maybe you’d put the remainders there, or maybe you would put booleans there for what the result was.

Was that right too?

And then, what would you do with all those booleans, how should they be combined to get a single answer?

For example:

> n = 37
> divisors = [2 .. n-1]
> divisors
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
> remainders = (n `mod`) <$> divisors
> remainders
[1,1,1,2,1,2,5,1,7,4,1,11,9,7,5,3,1,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
> any (== 0) remainders
False

Regardless of whether you use arrays or use the values immediately, the actions you make are ones that you can simultaneously write to screen so that you can check whether that’s the right thing.

1 Like