Lab 2: Leap Years, JUnit

Getting The Code / Submission Instructions

Click here to download the .zip file for the skeleton code for this lab. When you are finished, please submit the following two files on Canvas:

  • src/lab2_leapyears/Lab2_LeapYears.java
  • test/lab2_leapyears/Lab2_LeapYearsTest.java

Overview

Because the length of the Earth's orbit is actually about 365.256 days, we occasionally need leap years with 366 days to keep everything on track. The rules for leap years are as follows:

  • Every year that is not a centennial year, but which is evenly divisible by 4, is a leap year.
  • Only centennial years which are evenly divisible by 400 are leap years. So, for instance, the year 2000 was a leap year, and 2400 will be a leap year, but 1900 was not, nor will 2100, 2200, or 2300 be.

Code To Write

This assignment will give our first sneak preview of methods, or blocks of code that encapsulate a little task that might be run over and over again. Your task will be to fill in the method
public static boolean isALeapYear(int year)
Based on the input variable year, you should have some if statements that set the "return variable" (output variable of the method) isLeap to true if year is a leap year, and to false if it is not. Finally, you should add a statement in the main function to print "It's not a leap year" if it is not a leap year.

Testing

In addition to completing a program that takes as input a year and outputs whether or not it is a leap year, you will also be writing a series of test cases that will be run to make sure your program works properly. We will go over this more during the lab, and there is information about this in homework 2, but basically, you will write a series of methods that have an input value and what the output should be, and NetBeans will run these tests and make sure your code actually gives that output value. In this lab, you should add at least 4 of your own tests, two of which are leap years, and two of which are not. Be sure that your tests cover every block of code that you write.