### More complicated Boolean expressions

• A single Boolean expression to determine leap year

• Recall: Leap year description (Wikipedia):

 In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years

• Constructing a Boolean expression for "leap year":

• (most) years that are evenly divisible by 4 are leap years:

 ``` Year is leap year if: year % 4 == 0 (divisible by 4) ```

• Years that are evenly divisible by 100 are not leap years:

 ``` Years that are evenly divisible by 100 ==> (year % 100 == 0) They are NOT leap years !!! So: a year is leap if: divisible by 4 AND not divisible by 100 Conclusion (so far): (year % 4 == 0) && !(year % 100 == 0) ```

• Continued: unless they are also evenly divisible by 400, in which case they are leap years

 ``` Meaning: a year is divisible by 400 is ALWAYS leap So a year is leap year if: (whatever makes it leap) OR (divisible by 400) Result: ( (year % 4 == 0) && !(year % 100 == 0) ) || (year % 400 == 0) ```

BTW, because && has a higher priority than ||, we do not need the brackets:

 ``` (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ```

• Java program:

 ``` import java.util.Scanner; public class LeapYear02 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter year:"); year = in.nextInt(); // Read in year if ( (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ) { System.out.println("It is a leap year"); } else { System.out.println("It is NOT a leap year"); } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac LeapYear02.java To run:          java LeapYear02