When is a year a leap year

  • When is a year a leap year

      • Every year that is divisible by four (4) is a leap year, except for years that are divisible by 100, but these centurial years are leap years if they are divisible by 400.

        For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.

  • Alternative way to tell if a year is leap:

      • A year that is divisible by 400 is a leap year (e.g.: 1600 and 2000)

      • A year that is divisible by 4 but not divisible by 100 is also a leap year (e.g.: 1700, 1800, 1900 and not leap years)

Case study:    Read in a year and determine if it is a leap year

Start with a plan:



public class CheckLeapYear 
{
   public static void main(String[] args) 
   {

       
       Read in year
       
       Determine if year is a leap year;

     
   

       Print result
   }
}   

Case study:    Read in a year and determine if it is a leap year

Fill in the plan:

import java.util.Scanner;

public class CheckLeapYear 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int year = input.nextInt();

       Check if: 
           year is divisble by 4      AND
           (year is not divisble by 100 OR is divisible by 400)


       Print result   
   }
}   

Case study:    Read in a year and determine if it is a leap year

Write the condition in Java syntax:

import java.util.Scanner;

public class CheckLeapYear 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int year = input.nextInt();

       boolean isLeapYear;

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

       System.out.println( isLeapYear );
   }
}   

DEMO: demo/03-selections/07-case-study/CheckLeapYear.java

Case study:    Read in a year and determine if it is a leap year

Alternative way to check if a year is prime:

import java.util.Scanner;

public class CheckLeapYear 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int year = input.nextInt();

       Check if: 
           year is divisble by 400     OR
           year is divisble by 4 AND NOT by 100


       Print result   
   }
}   

Case study:    Read in a year and determine if it is a leap year

Write the condition in Java syntax:

import java.util.Scanner;

public class CheckLeapYear 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int year = input.nextInt();

       boolean isLeapYear;

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

       System.out.println( isLeapYear );
   }
}   

DEMO: demo/03-selections/07-case-study/CheckLeapYear2.java