Advertisement
CR7CR7

PrimeDirective

Mar 29th, 2023
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. // Import statement:
  2. import java.util.ArrayList;
  3. class PrimeDirective {
  4.  
  5.   // Add your methods here:
  6.   public boolean isPrime(int number){
  7.     for(int i=2;i<number;i++){
  8.  
  9.       if (number == 2) {
  10.   return true;
  11. } else if (number < 2) {
  12.   return false;
  13. }
  14.  
  15.     if(number%i==0){
  16.       return false;
  17.   }else{
  18.     return true;
  19.   }
  20.     }
  21.     return true;
  22.   }
  23.   public ArrayList<Integer> onlyPrimes(int[] numbers) {
  24.  
  25.   // method body goes here
  26.   ArrayList<Integer> primes = new ArrayList<Integer>();
  27.   for (int number : numbers) {
  28.     if(isPrime(number)){
  29.       primes.add(number);
  30.     }
  31. // returns true or false
  32.  
  33.   // do something with animal
  34.  
  35. }
  36.  return primes;
  37. }
  38.  
  39.   public static void main(String[] args) {
  40.  
  41.     PrimeDirective pd = new PrimeDirective();
  42.     int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
  43.  System.out.println(pd.isPrime(6));
  44.  System.out.println(pd.isPrime(28));
  45.  System.out.println(pd.isPrime(2));
  46.  System.out.println(pd.isPrime(0));
  47.  System.out.println(pd.onlyPrimes(numbers));
  48.  
  49.  
  50.  
  51.    
  52.  
  53.   }  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement