Problem 2.3

CIS 210 Home Page Last updated 2007/09/09 12:27:02

Problem 2.3 - Calculating Dates

In this problem you are to figure out the logic and write code to calculate values involving a date.

The Problem

The statement of this problem is fairly simple: Given a date between the years 1600 and 2400, we would like to know how far along in the year that date sits, and also what day of the week the date falls on.

For example, you might like to know what day of the week your birthday will be on next year, or maybe on which day of the week you were born. You might want to know the number of days until Christmas, and you could figure this out if you knew the positions of today and Christmas in the year.

What complicates this problem is our calendar system. For this problem, you will calculate the day of the year and the day of the week in the Gregorian calendar, which is the most widely used calendar. The complication of calendars is that we want the same astronomical events (like the vernal equinox) to occur on the same day each year. If the length of the year is not just right, then such an event will drift over the years, with the result that seasons would no longer be associated with particular months. The Gregorian calendar addresses this by adjusting the length of the year periodically, adding a day in leap years. Further complicating calculations is that months have varying numbers of days, either 30 or 31, or even 28 or 29 for February. See the Wikipedia entry for the Gregorian calendar for more details.

Leap years

In a leap year, the month of February has an extra day at the end: February 29. Leap years occur every four years to keep the calendar on track, compensating for the fact that a day is a few minutes short. At first glance, a leap year would be a year that is divisible by 4. However, this over corrects, so we skip the year every 100 years. But this would be too short again, so we keep the skipped leap year every 400 years. Thus, a leap year is a year divisible by 4, unless it is divisible by 100 and not by 400.

Day of the week

There are seven days of the week, Sunday through Saturday. One way to determine the day of the week is to start from one that is known. In this problem, you are given the fact that January 1, 1600 was a Saturday. From that fact you should be able to calculate the day of the week for any other date, and for this problem you only need to do that for dates from 1600 to 2400 (inclusive).

Starting Code

You should start with the following code FindDay.java. The main method has code that prompts for a date, which will be realized as three numerical values: the month, the day, and the year. Although we will cover methods more completely next week, your code for this assignment must be in the two methods: getDayOfYear and getDayOfWeek. This will make it easier for us to test and grade your code accurately. The code for these methods is started for you.

You should not change the main method.

In addition to prompting for a date, calling the methods and displaying the results, main also uses Java library classes to display the correct answers. As you are developing your code, you can use this information to see if your logic is right.

Coding Requirements

You cannot use Java library classes (other than String) in your code. You cannot use any loops in your code, and you cannot use arrays. You should be able to code a solution using only expressions, if-else statements, and/or switch statements. Even with these restrictions, you should be able to come up with a reasonably elegant solution.

Make sure you comment your code thoroughly so that we can understand your logic.

Error checking

Your code should check that the input is a proper date, i.e., all values are in range and make sense. (E.g., April 31 would be invalid in any year.) The error handling can be simple: just output an error message and terminate the program by calling System.exit(1);

Testing your solution

Test your solution with various values and compare your results to what is calculated by the Java library code. Remember to consider borderline cases - beginning and end of year, leap years, not leap years, etc.

Turn in

Your working version of FindDay.java.