KaiquanMah/TurkuBasicOOPinJava
0
1Write a class StringHelper, which has the following static methods:2int countVowels(String string), which returns the number of vowels in the given string3int countOthers(String string), which returns the number of all other characters except vowels in the given string4You can assume that all processed strings only contain lowercase letters.5 6 7 8 9import java.util.Random;10 11public class Test{12 public static void main(String[] args){13 final Random r = new Random();14 15 String[] words = ("john alphabets hellothere cheerio hi" +16 "open aaaaeeeeiiii grrrrr").split(" ");17 for (String word : words) {18 System.out.println("Testing with word " + word);19 System.out.println("Vowels: " + StringHelper.countVowels(word));20 System.out.println("Others: " + StringHelper.countOthers(word));21 } 22 }23}24 25 26 27 28 29//ADD30class StringHelper {31 public StringHelper() {}32 33 // STATIC METHOD134 public static int countVowels(String string) {35 int count = 0;36 for (int i = 0; i < string.length(); i++) {37 if (string.charAt(i) == 'a' ||38 string.charAt(i) == 'e' ||39 string.charAt(i) == 'i' ||40 string.charAt(i) == 'o' ||41 string.charAt(i) == 'u') {42 count++;43 }44 }45 return count;46 }47 48 // STATIC METHOD249 public static int countOthers(String string) {50 int count = 0;51 for (int i = 0; i < string.length(); i++) {52 if (string.charAt(i) != 'a' &&53 string.charAt(i) != 'e' &&54 string.charAt(i) != 'i' &&55 string.charAt(i) != 'o' &&56 string.charAt(i) != 'u') {57 count++;58 }59 }60 return count;61 }62}63 64 65 66 67 68Testing with word john69Vowels: 170Others: 371Testing with word alphabets72Vowels: 373Others: 674Testing with word hellothere75Vowels: 476Others: 677Testing with word cheerio78Vowels: 479Others: 380Testing with word hiopen81Vowels: 382Others: 383Testing with word aaaaeeeeiiii84Vowels: 1285Others: 086Testing with word grrrrr87Vowels: 088Others: 689 90 