added a bigger prime number to test and fixed the bug that one was a prime, since it is not.
public class Primes { public static boolean isAPrime(int number) { if(number > 1 && number == 2) return true; //1 is not a prime number by definition else { for(int i = 3; i*i < number; i +=2) { if (number % i == 0) return false; } } return true; } }
- public class Primes {
- public static boolean isAPrime(int number) {
if(number == 1 || number == 2) return true;- if(number > 1 && number == 2) return true; //1 is not a prime number by definition
- else {
- for(int i = 3; i*i < number; i +=2) {
- if (number % i == 0) return false;
- }
- }
- return true;
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void twoShouldBeAPrime() { int numberToCheck = 2; boolean expected = true; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void fortySevenShouldBeAPrime() { int numberToCheck = 47; boolean expected = true; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
- public void twoShouldBeAPrime() {
- int numberToCheck = 2;
- boolean expected = true;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- @Test
- public void fortySevenShouldBeAPrime() {
- int numberToCheck = 47;
- boolean expected = true;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- }