Java Lab #3: Decisions

  1. Write a program that accepts a date as three integers (year, month, and day) and the prints out the next date after the given date. A sample run might look like:

            Year : 2013
            Month: 9
            Day  : 3
    
            Tomorrow is: 2013-09-04
          

    I recommend beginning by creating a program that accepts the input (no input validation is necessary... yet) and just outputting the same date that it reads using appropriate formatting.

    In the first version of your program you can assume that all months have 31 days for simplicity. Your program should work by first adding one to the day (increment day) and then checking to see if the number of days has gone past 31. If so, it should set the day value back to 1 (start of the next month) and increment the month. If the month is then past 12 your program should set the month value back to 1 (start of the next year) and increment the year.

    Test your program on the input dates of 2013-02-31 and 2013-12-31. Verify the correct output.

  2. Enhance your program so that it deals with the fact that the months have different lengths. However, in this version don't worry about leap years. To do this you may find it convenient to use a switch statement:

            switch (month) {
            case 1:
              // It's January... check if day is too large for this month.
              break;
            case 2:
              // It's February... check if day is too large for this month.
              break;
            etc...
            }
          

    There is actually a better way to handle this but it requires a feature we haven't discussed yet (arrays). This approach will be manageable for this program.

    This version of the program should follow the pseudo-code below

            <Input a date from the user>
    
            <Increment day>
            SWITCH <on month>
                CASE <January>:
                    IF <day > 31> THEN
                        <Set day back to 1>
                        <Increment month>
                    END
                    BREAK
                -- etc for other months but with appropriate checks for each month
            END
    
            IF <month > 12> THEN
                <Set month back to 1>
                <Increment year>
            END
    
            <Output the new date>
          
  3. Enhance your program to account for leap years. The leap year rules used by our calendar are actually somewhat complicated:

    Try your revised program out on the following dates:

    InputExpected OutputComment
    2013-02-282012-03-01Ordinary year
    2012-02-282012-02-29Ordinary leap year
    1900-02-281900-03-01Application of 100 year rule
    2000-02-282000-02-29Application of 400 year rule
  4. Enhance your program one final time to provide input validation. If the user enters an invalid date the program should produce a reasonable error message and not attempt to calculate the next date. Be sure to handle leap years properly.