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
}
}
|
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
}
}
|
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
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
}
}
|
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