Advertisement
TS10315016

final_exam_03

May 29th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. package final_exam_03;
  2. import java.util.*;
  3.  
  4. interface Condition
  5. {
  6.     public abstract boolean satisfies(String s);
  7. }
  8.  
  9. class WordsLen implements Condition
  10. {
  11.     int length;
  12.    
  13.     public WordsLen(int length)
  14.     {
  15.         this.length = length;
  16.     }
  17.    
  18.     public boolean satisfies(String s)
  19.     {
  20.         if(this.length==s.length())
  21.         {
  22.             return true;
  23.         }
  24.         return false;
  25.     }
  26. }
  27.  
  28. class WordsTwiceInRow implements Condition
  29. {
  30.     public boolean satisfies(String s)
  31.     {
  32.         int count = 0;
  33.         for(int i=0;i<s.length()-1;i++)
  34.         {
  35.             if(s.charAt(i) == s.charAt(i+1))
  36.             {
  37.                 count++;
  38.             }
  39.         }
  40.        
  41.         if(count==0) return false;
  42.         else return true;
  43.     }
  44. }
  45.  
  46.  
  47.  
  48. public class final_exam_03
  49. {
  50.     public static ArrayList<String> countWords(String arr[] , Condition f)
  51.     {
  52.         ArrayList<String> r = new ArrayList<String>();
  53.         for(int i=0;i<arr.length;i++)
  54.         {
  55.             if(f.satisfies(arr[i]))
  56.             {
  57.                 r.add(arr[i]);
  58.             }
  59.         }
  60.         return r;
  61.     }
  62.    
  63.     public static void main(String[] args)
  64.     {
  65.         String arr[] = {"hello","world","this","if","that","too"};
  66.         System.out.println(countWords(arr,new WordsLen(5)));
  67.         System.out.println(countWords(arr,new WordsTwiceInRow()));
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement