LeapYear.java


Below is the syntax highlighted version of LeapYear.java from §11 Appendix.



/*************************************************************************
 *  Compilation:  javac LeapYear.java
 *  Execution:    java LeapYear N
 *  
 *  Prints true if N corresponds to a leap year, and false otherwise.
 *  Assumes N >= 1582, corresponding to a year in the Gregorian calendar.
 *
 *  % java LeapYear 2004
 *  true
 *
 *  % java LeapYear 1900
 *  false
 *
 *  % java LeapYear 2000
 *  true
 *
 *************************************************************************/

public class LeapYear { 
   public static void main(String[] args) { 
      int year = Integer.parseInt(args[0]);
      boolean a = (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0);
      boolean b = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
      boolean c = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
      boolean d = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
      boolean e = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
      System.out.println(d);
      System.out.println(e);
   }
}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.