-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise01.java
62 lines (53 loc) · 2.31 KB
/
Exercise01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class ExerciseOne {
public static void main(String args[]) {
ExerciseOne ex01 = new ExerciseOne();
String word = "redorangeyellowgreen";
System.out.println(word + " to uppercase is " + ex01.stringToUppercase(word));
System.out.println("The length of " + word + " is " + ex01.wordLength(word));
System.out.println("The index of yellow in " + word + " is " + ex01.stringIndexOf("yellow", word));
System.out.println("The word combo is " + ex01.wordCombo(word));
System.out.println("The number of \'e\' in " + word + " is " + ex01.freqOfLetterInString('e', word));
}
// Complete this method to return the word in all uppercase
private String stringToUppercase(String word) {
return word.toUpperCase();
}
// Complete this method to return the length of the word
private int wordLength(String word) {
return word.length();
}
// Complete this method to return the index of
// the specified substring in the given word
private int stringIndexOf(String subStr, String word) {
int index = word.indexOf(subStr);
return index;
}
// Complete this method to return a combination of
// the first three letters and the last four letters of the given word.
// E.g. given the word "redorangeyellowgreen", "redgreen" should be returned
private String wordCombo(String word) {
String first3 = word.substring(0, 3);
String last4 = word.substring(word.length() - 5, word.length());
word = first3 + last4;
return word;
}
// Complete this method to return the frequencey of
// the specified letter in the given word. If the specified letter is not
// in the given word, then return -1.
// E.g. given the word "redorangeyellowgreen",
// the frequency of 'e' in the word is 5
private int freqOfLetterInString(char letter, String word) {
int freq = 0;
// go throuch each character in word
// if the character is the same as the given letter,
// then add 1 to freq
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
freq += 1;
}
}
if (freq == 0) {
freq = -1;
}
return freq;
}}