-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththePrimeDirective.java
More file actions
46 lines (36 loc) · 994 Bytes
/
thePrimeDirective.java
File metadata and controls
46 lines (36 loc) · 994 Bytes
1
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
37
38
39
40
41
42
43
44
45
// Import 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;
}
for (int i = 2; i < number; i++){
if(number % i == 0) {
return false;
}
}
return true;
}
public ArrayList<Integer> onlyPrimes(int[] numbers) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for(int 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));
}
}