// Helene Martin, CSE 142 // Several methods that return booleans import java.util.*; public class BooleanDemo { 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 = true; if (canVote && isSunny) { System.out.println("To the polls!"); } if (!isPrime(age)) { System.out.println("Your age is NOT prime! =("); } System.out.println(bothOdd(5, 7)); // true System.out.println(bothOdd(6, 4)); // false System.out.println(bothOdd(5, 4)); // false System.out.println(bothOdd(0, 7)); // false System.out.println(bothOdd(1, 5)); // true System.out.println(bothOdd(-5, -7)); // true System.out.println(isVowel("i")); System.out.println(isVowel("q")); } // returns whether a value is a prime or not public static boolean isPrime(int value) { return factors(value) == 2; // if (factors == 2) { // return true; // } else { // return false; // } } // counts and returns the factors of a given number public static int factors(int value) { int factors = 0; for (int i = 1; i <= value; i++) { if (value % i == 0) { factors++; } } return factors; } // bothOdd returns true if both parameters are odd, false otherwise public static boolean bothOdd(int num1, int num2) { boolean num1Odd = num1 % 2 != 0; boolean num2Odd = num2 % 2 != 0; return num1Odd && num2Odd; } // isVowel returns whether a String is a vowel (a, e, i, o, or u), // case-insensitively. 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"); } // isNonVowel returns whether a String is any character except a vowel. public static boolean isNonVowel(String s) { //!s.equals("a") && !s.equals("e") ... use De Morgan's law return !isVowel(s); } }