CMPU 102 - Assignment 2 Due: by 11:59 PM on Friday, February 10th

$Revision: 1.2 $

This assignment is a review of primitive Java and classes/objects, and an introduction to testing Java classes with JUnit.

The Date Class

Implement a class called "Date" to represent a calendar date.  To get started, download and import it into your workspace.  (Instructions for loading a project into the Eclipse workspace can be found in the instructions for Lab 1.)  The Date class defined in this project provides a starting point for this assignment.

You will need to delete all of the statements that read:
throw new UnsupportedOperationException("TODO");
since those are just placeholders.

The methods you must implement are:

You will need to handle leap years.  Use the following rules to determine whether or not a particular year is a leap year:

All years evenly divisible by 4 are leap years, except years which are evenly divisible by 100 and not evenly divisible by 400.
For example, the year 2000 was a leap year, even though it is evenly divisible by 100, because it also evenly divisible by 400.

Testing with JUnit

The class DateTest defines unit tests for the Date class.  A unit test is a small fragment of code that tests part of the functionality of a class.  A class containing unit tests is called a test suite.

Each of the methods in DateTest whose name starts with "test" is a unit test.  For example, the testIsLeapYear method tests the Date class's isLeapYear method:

public void testIsLeapYear() {

Each unit test contains calls to static methods in the junit.framework.Assert class.  These methods check that an expected condition is true: in this particular unit test, they check that the isLeapYear method returns true for the year 2004 and false for the year 2006.

Typically, you will need objects to be created in order to test the instance methods of a class.  When using JUnit, the test suite class should define fields in which to store references to the objects you want to test.  The setUp method of a test suite class is responsible for creating these objects and assigning them to the fields.  For example, the setUp method of DateTest creates one object:

protected void setUp() throws Exception {
    dateAssigned = new Date(2, 2, 2006);

Unit test methods can then use this object:

public void testGetDay() {
    Assert.assertEquals(2, dateAssigned.getDay());

The Assert class contains a number of overloaded variants of the assertEquals method for testing the equality of values of various types.

Eclipse makes it easy to run all of the unit tests defined in a test suite class.  Just right click on the name of the source file ("") and choose "Run As->JUnit Test".  You will see a window similar to the following:

A completely green bar means that all of the unit tests passed.  If one or more unit tests fail, you will see a window looking something like this:

In this example, the testIsLeapYear unit test failed because one of the assertions made in the test was false.

Your Task

Your task is to implement the Date class, and to extend the DateTest class to add additional unit tests.  In particular, you will want to develop tests for special cases, such as years which are multiples of 100 or 400.

Some guidelines for writing unit tests:

  1. The names of all unit test methods must begin with "test"

  2. Give your unit test methods meaningful names

  3. Keep the number of assertions in each unit test method small

  4. Once you have created an object in setUp, don't change the way it is constructed.  Instead, create a different object an initialize it differently.

Submitting your solution

From a terminal window, type the following commands:

cd eclipse-workspace
submit102 assign2