// Helene Martin, CSE 142 // Methods that return booleans. import java.util.*; public class BooleanExamples { public static void main(String[] args) { System.out.println(bothOdd(3, 5)); //true System.out.println(bothOdd(2, 3)); //false System.out.println(bothOdd(0, 2)); //false System.out.println(isConsonant('i')); //false System.out.println(isConsonant('o')); //false System.out.println(isConsonant('b')); //true System.out.println(isConsonant('t')); //true System.out.println(hasVowel("aaaalaaaaaa")); //true System.out.println(hasVowel("aaaaaaaaaa")); //true System.out.println(hasVowel("qqqqqfffa")); //true System.out.println(hasVowel("qqqqffflll")); //false System.out.println(isAllVowels("aaaaaaaaaa")); //true System.out.println(isAllVowels("aaaaaeeeiiiiaaaaa")); //true System.out.println(isAllVowels("aaaalaaaaaa")); //false for (int i = 1; i <= 50; i++) { if (isPrime(i)) { System.out.print(i + " "); } } System.out.println(); } // returns true if prime, false otherwise public static boolean isPrime(int number) { return countFactors(number) == 2; //boolean result = countFactors(number) == 2; /*if (result == true) { return true; } else { return false; }*/ } // returns the count of factors that number has public static int countFactors(int number) { int counter = 0; for (int i = 1; i <= number; i++) { if (number % i == 0) { counter++; } } return counter; } // both odd: true if both parameters are odd, false otherwise public static boolean bothOdd(int a, int b) { return a % 2 == 1 && b % 2 == 1; /*if (a % 2 == 1 && b % 2 == 1) { return true; } else { return false; }*/ } // prints non-vowel letters from the phrase // e.g.: vowels -> vwls public static void printConsonants(String phrase) { for (int i = 0; i <= phrase.length() - 1; i++) { char c = phrase.charAt(i); if (isConsonant(c)) { System.out.print(c); } } } // isConsonant: true if the letter passed in is a consonant, false otherwise public static boolean isConsonant(char c) { return !isVowel(c); } // isVowel: true if the letter passed in is a vowel, false otherwise public static boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } // hasVowel: true if the string contains a vowel, false otherwise public static boolean hasVowel(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (isVowel(c)) { return true; } } return false; // don't know that there won't be a vowel // until we've looked at ALL the letters } // isAllVowels: true if the string contains only vowels, false otherwise public static boolean isAllVowels(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (!isVowel(c)) { return false; } } return true; } }