Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package final_exam_03;
- import java.util.*;
- interface Condition
- {
- public abstract boolean satisfies(String s);
- }
- class WordsLen implements Condition
- {
- int length;
- public WordsLen(int length)
- {
- this.length = length;
- }
- public boolean satisfies(String s)
- {
- if(this.length==s.length())
- {
- return true;
- }
- return false;
- }
- }
- class WordsTwiceInRow implements Condition
- {
- public boolean satisfies(String s)
- {
- int count = 0;
- for(int i=0;i<s.length()-1;i++)
- {
- if(s.charAt(i) == s.charAt(i+1))
- {
- count++;
- }
- }
- if(count==0) return false;
- else return true;
- }
- }
- public class final_exam_03
- {
- public static ArrayList<String> countWords(String arr[] , Condition f)
- {
- ArrayList<String> r = new ArrayList<String>();
- for(int i=0;i<arr.length;i++)
- {
- if(f.satisfies(arr[i]))
- {
- r.add(arr[i]);
- }
- }
- return r;
- }
- public static void main(String[] args)
- {
- String arr[] = {"hello","world","this","if","that","too"};
- System.out.println(countWords(arr,new WordsLen(5)));
- System.out.println(countWords(arr,new WordsTwiceInRow()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement