Spaces:
Running
Running
Download Week 6: Methods of OO Programming/01B. Class StringHelper from KaiquanMah/TurkuBasicOOPinJava: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/spaces/KaiquanMah/TurkuBasicOOPinJava/resolve/main/Week%206%3A%20Methods%20of%20OO%20Programming/01B.%20Class%20StringHelper
- Command line
-
hf download 'hf://spaces/KaiquanMah/TurkuBasicOOPinJava/Week 6: Methods of OO Programming/01B. Class StringHelper'
-
curl -L -o '01B. Class StringHelper' https://huggingface.co/spaces/KaiquanMah/TurkuBasicOOPinJava/resolve/main/Week%206%3A%20Methods%20of%20OO%20Programming/01B.%20Class%20StringHelper
2.15 kB
| 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 | |