// Helene Martin, CSE 142 // Short methods illustrating boolean returns // Remember boolean zen! import java.util.*; public class BooleanExamples { public static void main(String[] args) { /*Scanner s = new Scanner(System.in); System.out.print("How old are you? "); int age = s.nextInt(); boolean canVote = age >= 18; boolean isSunny = false; // true and false are keywords, do NOT put in quotes System.out.println(canVote); if(canVote && isSunny) { System.out.println("To the polls!"); } if(isPrime(age)) { System.out.println("Your age is prime!"); } 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")); //true System.out.println(isConsonant("o")); //true System.out.println(isConsonant("b")); //false System.out.println(isConsonant("t")); //false System.out.println(isAllVowels("aaaaaaaaaa")); //true System.out.println(isAllVowels("aaaaaeeeiiiiaaaaa")); //true System.out.println(isAllVowels("aaaalaaaaaa")); //false System.out.println(hasNoVowel("aaaalaaaaaa")); //false System.out.println(hasNoVowel("aaaaaaaaaa")); //false System.out.println(hasNoVowel("qqqqqfffa")); //false System.out.println(hasNoVowel("qqqqffflll")); //true */ } // true if the string contains no vowels, false otherwise public static boolean hasNoVowel(String word) { for(int i = 0; i < word.length(); i++) { String letter = word.substring(i, i + 1); if(isVowel(letter)) { return false; } } return true; } // true if the string contains only vowels, false otherwise public static boolean isAllVowels(String word) { for(int i = 0; i < word.length(); i++) { String letter = word.substring(i, i + 1); if(!isVowel(letter)) { return false; } } return true; } // true if the letter passed in is a vowel, false otherwise public static boolean isVowel(String l) { return l.equalsIgnoreCase("a") || l.equalsIgnoreCase("e") || l.equalsIgnoreCase("i") || l.equalsIgnoreCase("o") || l.equalsIgnoreCase("u"); } // true if the letter passed in is a consonant public static boolean isConsonant(String l) { return !isVowel(l); } // true if both parameters are odd, false otherwise public static boolean bothOdd(int n1, int n2) { return n1 % 2 == 1 && n2 % 2 == 1; } // returns whether num is prime public static boolean isPrime(int num) { return countFactors(num) == 2; } // 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; } }