Assignment 02: Dates
Fall 2007, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Your program is going to ask the user which of three things to do:

Details:

Start with the following outline. Create a project in Eclipse named Dates. Create a class in that project, also called Dates. Then copy and paste the following code into the Dates class, replacing the initial code that Eclipse created for you.

/**
 * Assignment 2 for CIT591, Fall 2007. 
 * @author Dave Matuszek
 * @author Replace this with your name!
 * @author Replace this with your partner's name!
 * @version Sep 11, 2007
 */
public class Dates {

    /**
     * Computes day of the week for specific dates, and the number
     * of days between two dates.
     * 
     * @param args Not used.
     */
    public static void main(String[] args) {
        new Dates().run();
    }

    /**
     * Asks the user whether to find and print the day of the week,
     * find and print the number of days from one date to the next,
     * or quit.
     */
    private void run() {
        boolean quitting = false;
        while (!quitting) {
            // Ask the user whether to: find and print the day of the week,
            // find and print the number of days from one date to the next,
            // or quit. If quitting, set the boolean variable 'quitting'
            // to true.
        }
    }
    
    /**
     * Asks the user for a specific date, determines what day of
     * the week it falls on, and prints that day.
     */
    public void findAndPrintDayOfWeek() {
        // Ask the user for a date (year, month, day)
        // Call findDayOfWeek to find what day of the week it is
        // Print the result
    }
    
    /**
     * Determines what day of the week a given date falls on.
     * 
     * @param year The year.
     * @param month The month (January = 1, December = 12).
     * @param day The day of the month.
     * @return One of the Strings "Sunday", "Monday", "Tuesday",
     *      "Wednesday", "Thursday", "Friday", or "Saturday".
     */
    public String findDayOfWeek(int year, int month, int day) {
        return null; // TODO replace this with the name of the weekday
    }
    
    /**
     * Asks the user for two dates, determines how far apart
     * (in days) those dates are, and prints the result.
     */
    public void findAndPrintDaysApart() {

    }
    
    /**
     * Determines how far apart (in days) two given dates are.
     * If the two dates are the same, the result is zero; if
     * the second date occurs before the first date, the result
     * is negative.
     * 
     * @param year1 The year of the first date.
     * @param month1 The month of the first date.
     * @param day1 The day of the month of the first date.
     * @param year2 The year of the second date.
     * @param month2 The month of the second date
     * @param day2 The day of the month of the second date.
     */
    public int findDaysApart(int year1, int month1, int day1,
                              int year2, int month2, int day2) {
        return 0; // TODO replace this with the correct value
    }
}

Use exactly the class name, method names, and method parameters as given. You can create additional methods, but you must also have the given ones as specified. Any additional methods should also have Javadoc comments.

Day of the week:

Here is the month code table:

Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov

Dec

0
(6 in leap years)
3
(2 in leap years)
3
6
1
4
6
2
5
0
3
5

Here is the algorithm you should use.

Algorithm Example
  1. Take any year (including the century, eg. 1998 not 98).
  2. Divide the year by 4 and ignore any remainder. Add this result onto the year, giving a total.
  3. Divide the year by 100, ignore any remainder and subtract this from the total.
  4. Divide the year by 400, ignore any remainder and add this to the total.
  5. Add the date (day of the month).
  6. Add the month code from the above table.
  7. Subtract 1 from the total.
  8. Divide by 7 and take the remainder.
  9. This gives you the day of the week (0 is Sunday, 1 is Monday, etc.).
  1. Start with Sept. 17, 2007
  2. 2007/4 = 501, 2007 + 501 = 2508
  3. 2007/100 = 20, 2508 - 20 = 2488
  4. 2007/400 = 5, 2488 + 5 = 2493
  5. 2493 + 17 = 2510
  6. Sept. = 5, 2510 + 5 = 2515
  7. 2515 - 1 = 2514
  8. 2514 % 7 = 1   (7 * 359 + 1)
  9. 1 = Monday

The day of the week should be one of the strings "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday". Please get the spelling correct.

Time between two dates:

Here's the easiest way to do this:

  1. Make sure the first date is earlier than or equal to the second date.
    1. If this isn't the case, rearrange the dates and remember that you did so.
  2. If the two dates are in the same year,
    1. Find the day of the year (1..366) of each date, and subtract.
  3. If the two dates are in different years,
    1. Find the number of days remaining in the first year (number of days in year, minus day of the year).
    2. Add the day of the year of the second year.
    3. Add the number of days in each intervening year (365 or 366, depending on the year).
If both dates are the same, the "time between" should be zero. If the dates are consecutive, the "time between" should be one. If you had to rearrange the dates, it's desirable if you indicate this in some way--but since I forgot to say that earlier, it's optional.

General:

Java has some built-in classes for working with dates. You are not permitted to use them for this assignment; I want you to do the computations yourself.

This assignment has been designed so that you and your partner need to work together on part of it, and can do the other parts separately. However, you should work together as closely as feasible; in particular, you should read and test each other's code before you submit the project.

There is a method for computing leap years on my slides; feel free to use it.

Good advice: Once you have tested your program and made sure it works, submit it without making any additional changes. Errors can creep in at the last minute, even if all you think you are doing is adding or changing comments.

Due date:

Thursday, September 20, before midnight. Turn in your program electronically, using Blackboard. (See my Instructions for Using Zip Files and Blackboard). Only assignments submitted via Blackboard will be accepted--do not send your program by email. The usual late penalty of 5 points per day (out of 100 points) will apply.