KaiquanMah commited on
Commit
3ec1443
·
verified ·
1 Parent(s): 3d70db8

public static int method1(dtype param1)

Browse files
Week 6: Methods of OO Programming/01B. Class StringHelper ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a class StringHelper, which has the following static methods:
2
+ int countVowels(String string), which returns the number of vowels in the given string
3
+ int countOthers(String string), which returns the number of all other characters except vowels in the given string
4
+ You can assume that all processed strings only contain lowercase letters.
5
+
6
+
7
+
8
+
9
+ import java.util.Random;
10
+
11
+ public 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
+ //ADD
30
+ class StringHelper {
31
+ public StringHelper() {}
32
+
33
+ // STATIC METHOD1
34
+ 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 METHOD2
49
+ 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
+
68
+ Testing with word john
69
+ Vowels: 1
70
+ Others: 3
71
+ Testing with word alphabets
72
+ Vowels: 3
73
+ Others: 6
74
+ Testing with word hellothere
75
+ Vowels: 4
76
+ Others: 6
77
+ Testing with word cheerio
78
+ Vowels: 4
79
+ Others: 3
80
+ Testing with word hiopen
81
+ Vowels: 3
82
+ Others: 3
83
+ Testing with word aaaaeeeeiiii
84
+ Vowels: 12
85
+ Others: 0
86
+ Testing with word grrrrr
87
+ Vowels: 0
88
+ Others: 6
89
+