fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. interface VowelFunction{
  6. boolean isVowel(char ch);
  7. }
  8.  
  9. class EnglishVowelFunction implements VowelFunction{
  10. public boolean isVowel(char ch){
  11. switch(Character.toLowerCase(ch)){
  12. case 'a': return true;
  13. case 'e': return true;
  14. case 'i': return true;
  15. case 'o': return true;
  16. case 'u': return true;
  17. }
  18. return false;
  19. }
  20. }
  21.  
  22. interface StringCounter{
  23. int count(String s);
  24. }
  25.  
  26. abstract class VowelCounter implements StringCounter{
  27. VowelFunction vowelFunc;
  28.  
  29. private boolean isVowel(char ch){
  30. return vowelFunc.isVowel(ch);
  31. }
  32. public int count(String s){
  33. int count = 0;
  34. for(char ch: s.toCharArray()){
  35. if(isVowel(ch)){
  36. count++;
  37. }
  38. }
  39. return count;
  40. }
  41. }
  42.  
  43. class EnglishVowelCounter extends VowelCounter{
  44. EnglishVowelCounter(){
  45. vowelFunc = new EnglishVowelFunction();
  46. }
  47. }
  48.  
  49. //class TestVowelCount
  50. class Ideone{
  51. public static void main (String[] args) throws java.lang.Exception{
  52. String[] strs = {
  53. "abcdefghij",
  54. "あげぽよ",
  55. "アメンボあかいなあいうえお",
  56. "隣の客はよく柿食う客だ",
  57. "Köln",
  58. "为了容易理解地说明某事物而引用的类似的东西",// https://c...content-available-to-author-only...o.jp/content/%E4%BE%8B%E6%96%87
  59. };
  60.  
  61. for(String s: strs){
  62. StringCounter vowelCounter = new EnglishVowelCounter();
  63. int count = vowelCounter.count(s);
  64.  
  65. System.out.println(s);
  66. System.out.println("vowels:"+count);
  67. }
  68. }
  69. }
  70.  
Success #stdin #stdout 0.13s 53616KB
stdin
Standard input is empty
stdout
abcdefghij
vowels:3
あげぽよ
vowels:0
アメンボあかいなあいうえお
vowels:0
隣の客はよく柿食う客だ
vowels:0
Köln
vowels:0
为了容易理解地说明某事物而引用的类似的东西
vowels:0