/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package sixthfall16leapyear; import java.util.Scanner; /** * * @author jcmtiernan */ public class SixthFall16LeapYear { /** * @param args the command line arguments */ public static void main(String[] args) { int year = 0; boolean leap = false; //isALeapYear System.out.print("Please input a 4 digit year greater than 1201: "); Scanner input = new Scanner(System.in); boolean invalidYear = true; while ( invalidYear) { if (input.hasNextInt()) // is the user input an integer? { year = input.nextInt(); // read the input year invalidYear = isValidYear(year); } else { System.out.print("Your input is not an integer. Please re-enter: "); input.next(); System.out.println(); } } // year = input.nextInt(); leap = isLeapYear(year); // no else condition needed because not action is needed //If false, year is NOT a leap year // ( test condition ? true result : false result ) // ( a > b ? a : b ) System.out.println("Your input year "+year+" is "+(leap ? "" : "NOT ")+"a leap year"); if (leap) { System.out.println("February has 29 days in "+year); } else { System.out.println("February has 28 days in "+year); } } public static boolean isLeapYear(int yr) { boolean lp = false; //If the remainder of year divided by 4 is equal to 0 //might be a leap year if true if ((yr % 4) == 0) { System.out.println("Year is divisible by 4 "); //If true, then if year divided by 100 has a remainder of 0 if ((yr % 100) == 0) { System.out.println("Year is divisible by 100 "); //If true, then if year divided by 400 has a remainder of 0 if ((yr % 400) == 0) { System.out.println("Year is divisible by 400 "); //If true, year is a leap year lp = true; } //If false, year is NOT a leap year } else // remainder of year / 100 is not 0 { System.out.println("Year is NOT divisible by 100 "); //If false, year is a leap year lp = true; } } yr = 0; return lp; } public static boolean isValidYear(int year) { boolean invalidYear = true; System.out.println(); if (year > 1201) { invalidYear = false; } else { System.out.print("Your input is invalid. Please re-enter: "); } return invalidYear; } }