This attempted solution to Self-Check 5.15 (isVowel) has
several problems:
// Returns whether the given string represents a vowel:
// a, e, i, o, or u, case insensitively.
public static boolean isVowel(String s) {
if (s == "a") {
return true;
} else if (s == "e") {
return true;
} else if (s == "i") {
return true;
} else if (s == "o") {
return true;
} else if (s == "u") {
return true;
} else {
return false;
}
}
Open Practice-It from the link above, copy/paste this code into it, then see the next slide.
Fix the following aspects of the code:
public static boolean isVowel(String s) {
s = s.toLowerCase();
if (s.equals("a") || s.equals("e") || s.equals("i")
|| s.equals("o") || s.equals("u")) {
return true;
} else {
return false;
}
}
The above can be improved. "Boolean Zen" version:
public static boolean isVowel(String s) {
s = s.toLowerCase();
return s.equals("a") || s.equals("e") || s.equals("i")
|| s.equals("o") || s.equals("u");
}