Title: Calculating Dates Break Down
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

(10 points) Calculating Dates Break Down

Often beginning Java programmers tend to put too much code in main and things get complicated pretty quicky. In this exercise, you will see a program that has this bad characteristic: FutureDateTooMuchInMain.java. The goal is for you to see and understand a much better way to write programs. When you finish this exercise, make sure you go over it to see what has been accomplished by taking an approach of breaking the problem down into methods instead of one big hunk of code in main.
You will see a template(FutureDate.java) for creating a simpler program where the logic is broken into methods. Your job is to complete the logic. Fill in code where you see:
// ***************** Fill in the logic **************************
Both of these programs allow a user to enter in a year and a number of days from the beginning of the year. These 2 programs then compute the effective date. If a negative value is entered for the number of days then the program terminates. Here is some sample output from these 2 programs:
Sample Output:

Enter year
2014
Enter number of days after the year beginning( < 0 exits program):
0
January/1/2014
Enter year
2014
Enter number of days after the year beginning( < 0 exits program):
50
February/20/2014
Enter year
2014
Enter number of days after the year beginning( < 0 exits program):
70
March/12/2014
Enter year
2014
Enter number of days after the year beginning( < 0 exits program):
300
October/28/2014
Enter year
1978
Enter number of days after the year beginning( < 0 exits program):
200
July/20/1978
Enter year
1948
Enter number of days after the year beginning( < 0 exits program):
100
April/10/1948
Enter year
1948
Enter number of days after the year beginning( < 0 exits program):
10000
May/19/1975
Enter year
2014
Enter number of days after the year beginning( < 0 exits program):
-1
Program exitting





FutureDateTooMuchInMain.java

This program is complete but has too much code in main.

import java.util.Scanner;


public class FutureDateTooMuchInMain {
    public static void main(String[] args) {
        int startingYear, numDays;


        Scanner keyboard = new Scanner(System.in);
        do
        {
            System.out.println("Enter year ");
            startingYear = keyboard.nextInt();
            System.out.println("Enter number of days after the year beginning( < 0 exits program):");
            numDays = keyboard.nextInt();
            if (numDays >= 0)
            {
                int year = startingYear;
                int day = 1;
                int month = 1;
                boolean continueLoop=true;

                day += numDays;
                while (continueLoop)
                {
                    int daysInMonth;
                    switch(month)
                    {
                    case 4: case 6: case 9: case 11:
                        daysInMonth = 30;
                        break;
                    case 2:
                        if (year % 4 != 0) // not divisible by 4
                            daysInMonth=28;
                        else if (year % 100 != 0) // not divisible by 100
                            daysInMonth=29;
                        else if (year % 400 == 0) // divisible by 400
                            daysInMonth=29;
                        else
                            daysInMonth=28; // divisible by 100 but not 400

                        break;
                    default:
                        daysInMonth=31;
                        break;
                    }
                    if (day <= daysInMonth)
                    {
                        continueLoop = false;//not really needed ... done to clarify actions
                        break;
                    }
                    day -= daysInMonth;
                    month +=1;
                    if (month > 12)
                    {
                        year += 1;
                        month = 1;
                    }
                } // end of while (continueLoop)

                String monthName="";
                switch(month)
                {
                case 1:
                    monthName= "January";
                    break;
                case 2:
                    monthName= "February";
                    break;
                case 3:
                    monthName= "March";
                    break;
                case 4:
                    monthName= "April";
                    break;
                case 5:
                    monthName= "May";
                    break;
                case 6:
                    monthName= "June";
                    break;
                case 7:
                    monthName= "July";
                    break;
                case 8:
                    monthName= "August";
                    break;
                case 9:
                    monthName= "September";
                    break;
                case 10:
                    monthName= "October";
                    break;
                case 11:
                    monthName= "November";
                    break;
                case 12:
                    monthName= "December";
                    break;
                default:
                    monthName= "Unknown";
                    break;
                }// end of switch

                System.out.println( monthName +"/"+ day +"/" + year );
            }// end of if (numDays >= 0)


        } while (numDays >= 0);

        System.out.println("Program exitting");


    }


}


Template(Starting code) for a better program (you need to complete it):

FutureDate.java

import java.util.Scanner;


public class FutureDate {

    boolean isLeapYear(int year)
    {
        if (year % 4 != 0) // not divisible by 4
            return false;
        else if (year % 100 != 0) // not divisible by 100
            return true;
        else if (year % 400 == 0) // divisible by 400
            return true;
        else
            return false; // divisible by 100 but not 400
    }

    int daysInMonth(int monthNumber, int year)
    {
// *****************  Fill in the logic **************************
        // This routine returns the number of days in a month:
        // For monthNumber (4, 6, 9, or 11) we return 30 ... these months are
        //             April, June, September and November
        // For monthNumber 2 (February), we return 29 for leap years and 28 otherwise
        // We call isLeapYear(year) to find out whether this is a leap year or not.
        // All other monthNumbers will result in returning 31.


    }
    String monthName(int num)
    {
// *****************  Fill in the logic **************************
        // The method returns "January" if num==1, "February" if num==2, etc.
        // It returns "Unknown if num is not in the range between 1 and 12.
    }

    void computeDate(int year, int numDays)
    {
        int day = 1;
        int month = 1;

        day += numDays;
        while (day > daysInMonth(month, year))
        {
            day -= daysInMonth(month, year);
            month +=1;
            if (month > 12)
            {
                year += 1;
                month = 1;
            }
        }
        System.out.println( monthName(month) +"/"+ day +"/" + year );

    }

    public static void main(String[] args) {
        int startingYear, numDays;
        FutureDate fd = new FutureDate();

        Scanner keyboard = new Scanner(System.in);
        do
        {
            System.out.println("Enter year ");
            startingYear = keyboard.nextInt();
            System.out.println("Enter number of days after the year beginning( < 0 exits program):");
            numDays = keyboard.nextInt();
            if (numDays >= 0)
                fd.computeDate(startingYear, numDays);

        } while (numDays >= 0);
        System.out.println("Program exitting");

    }

}



Last Modified: December 6, 2014 2:40 PM