CS 1063, Fall 2005
Year class (for Leap Year)

The lectures for Week 11 had you work on an Year class that would determine if a year was a leap year (has a February 29 and 366 days in it) or not. Again for practice, I'm showing a YearTest class that inputs an ints without using Java 1.5.

The code below gives 4 different versions of the isLeapYear() method, two on each side. Of course you have to move the "2" on the name from one to the other for testing.

File: Year.java
// Year: check if leap year
public class Year {
   int myYear;
   
   public Year(int year) {
      myYear = year;
   }
   
   public boolean isLeapYear2() {
      boolean leap;
      // you must have the three else's below
      if (myYear%400 == 0) leap = true;
      else if (myYear%100 == 0) leap = false;
      else if (myYear%4 == 0) leap = true;
      else leap = false;
      
      return leap;
   }
   
   public boolean isLeapYear() {
      boolean leap = false;
      // you must NOT have else's below
      if (myYear%4 == 0) leap = true;
      if (myYear%100 == 0) leap = false;
      if (myYear%400 == 0) leap = true;
      
      return leap;
   }

   public String toString() {
      return "Year: " + myYear +
         ", is it a leap year? " + isLeapYear();
   }

}
// Year: check if leap year
public class Year {
   int myYear;
   
   public Year(int year) {
      myYear = year;
   }
   
   public boolean isLeapYear2() {
      // must be in the order given
      if (myYear%400 == 0) return true;
      else if (myYear%100 == 0) return false;
      else if (myYear%4 == 0) return true;
      else return false;
   }
   
   public boolean isLeapYear() {
      // must be in the order given
      // same as above, without else's
      if (myYear%400 == 0) return true;
      if (myYear%100 == 0) return false;
      if (myYear%4 == 0) return true;
      return false;
   }

   public String toString() {
      return "Year: " + myYear +
         ", is it a leap year? " + isLeapYear();
   }

}

Here we are inputting the three coefficients of a quadratic equation without using Java 1.5.

File: YearTest.java
import java.io.*;

public class YearTest {

   public static void main(String[] args)  throws IOException {
      BufferedReader infile =
         new BufferedReader( new InputStreamReader(System.in));
      System.out.print("Type an integer for year: ");
      String aLine = infile.readLine();
      int year = Integer.parseInt(aLine);
      Year y = new Year(year);
      System.out.println(y);
   }
}

Each of the 4 versions of isLeapYear() produces the following output.

Results of multiple runs
Type an integer for year: 1900
Year: 1900, is it a leap year? false

Type an integer for year: 1984
Year: 1984, is it a leap year? true

Type an integer for year: 1996
Year: 1996, is it a leap year? true

Type an integer for year: 1997
Year: 1997, is it a leap year? false

Type an integer for year: 2000
Year: 2000, is it a leap year? true

Type an integer for year: 2001
Year: 2001, is it a leap year? false

Type an integer for year: 2004
Year: 2004, is it a leap year? true

Type an integer for year: 2100
Year: 2100, is it a leap year? false