// Zorah Fung, CSE 142 // Examples of methods that return booleans public class BooleanMethods { public static void main(String[] args) { System.out.println("banana: " + hasVowel("banana")); System.out.println("crwth: " + hasVowel("crwth")); } // Returns true if the given word contains a vowel, false otherwise public static boolean hasVowel(String word) { word = word.toLowerCase(); for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); char c2 = word.substring(i, i + 1).charAt(0); if (isVowel(c)) { return true; } } return false; } // Returns true if the given lowercase letter is a vowel, false otherwise public static boolean isVowel(char c) { boolean vowel = c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; return vowel; } // Returns true if the given lowercase letter is not a vowel (consonant), false otherwise public static boolean isNonVowel(char c) { // Equivalent to !isVowel(c) // In order to negate the expression from isVowel, we both negate the tests // in each expression, as well as flip the logical operator from || to && using // DeMorgan's Law return c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u'; } }