Spaces:
Running
Running
| Write a class StringHelper, which has the following static methods: | |
| int countVowels(String string), which returns the number of vowels in the given string | |
| int countOthers(String string), which returns the number of all other characters except vowels in the given string | |
| You can assume that all processed strings only contain lowercase letters. | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| String[] words = ("john alphabets hellothere cheerio hi" + | |
| "open aaaaeeeeiiii grrrrr").split(" "); | |
| for (String word : words) { | |
| System.out.println("Testing with word " + word); | |
| System.out.println("Vowels: " + StringHelper.countVowels(word)); | |
| System.out.println("Others: " + StringHelper.countOthers(word)); | |
| } | |
| } | |
| } | |
| //ADD | |
| class StringHelper { | |
| public StringHelper() {} | |
| // STATIC METHOD1 | |
| public static int countVowels(String string) { | |
| int count = 0; | |
| for (int i = 0; i < string.length(); i++) { | |
| if (string.charAt(i) == 'a' || | |
| string.charAt(i) == 'e' || | |
| string.charAt(i) == 'i' || | |
| string.charAt(i) == 'o' || | |
| string.charAt(i) == 'u') { | |
| count++; | |
| } | |
| } | |
| return count; | |
| } | |
| // STATIC METHOD2 | |
| public static int countOthers(String string) { | |
| int count = 0; | |
| for (int i = 0; i < string.length(); i++) { | |
| if (string.charAt(i) != 'a' && | |
| string.charAt(i) != 'e' && | |
| string.charAt(i) != 'i' && | |
| string.charAt(i) != 'o' && | |
| string.charAt(i) != 'u') { | |
| count++; | |
| } | |
| } | |
| return count; | |
| } | |
| } | |
| Testing with word john | |
| Vowels: 1 | |
| Others: 3 | |
| Testing with word alphabets | |
| Vowels: 3 | |
| Others: 6 | |
| Testing with word hellothere | |
| Vowels: 4 | |
| Others: 6 | |
| Testing with word cheerio | |
| Vowels: 4 | |
| Others: 3 | |
| Testing with word hiopen | |
| Vowels: 3 | |
| Others: 3 | |
| Testing with word aaaaeeeeiiii | |
| Vowels: 12 | |
| Others: 0 | |
| Testing with word grrrrr | |
| Vowels: 0 | |
| Others: 6 | |