Title: Common Birthdays Example(using Date class)
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Common Birthdays(using Date class): Array Example

In this associated video, we will start with the previous "Common Birthday solution based on an integer date" solution.

To solve this with a Date, class I have provided an appropriate Date class below. It is similar to the author's Date class in chapter 4, but I have added the daysInMonth( ) method. The contents of what was used can be seen below after the "CommonBirthday" solution.

In the video, we start from the previous CommonBirthday logic where the date is represented as an integer between 1 and 365.

We "morph" this example to use the Date class shown below.

The code to solve this was developed on the video and the final program is shown below:

public class CommonBirthday {

    Date[] createBirthdayRoom(int num)
    {
        Date[] birthdays = new Date[num];
        for (int i=0; i < num; i++)
        {
            Date d = new Date();
            d.setYear(     (int)(1990 + 23*Math.random()));

            d.setMonth((int)(1 + 12*Math.random()));
            int numDaysInMonth = d.daysInMonth();
            d.setDay((int)(1 + numDaysInMonth*Math.random()));
            birthdays[i] = d;
        }
        return birthdays;
    }

    boolean commonBirthday(Date[] birthdays)
    {
        for (int i=1; i < birthdays.length; i++)
        {
            for (int k = 0; k < i; k++)
            {
                if ((birthdays[k].getDay() == birthdays[i].getDay()) &&
                        (birthdays[k].getMonth() == birthdays[i].getMonth()))
                    return true;
            }
        }
        return false;
    }

    double simulate(int numSims, int roomSize)
    {
        int success = 0;
        for (int i=0; i < numSims; i++)
        {
            Date[] br = createBirthdayRoom(roomSize);
            if (commonBirthday(br))
                success += 1;
        }
        return success/(double)numSims;
    }


    public static void main(String[] args) {
        CommonBirthday cb = new CommonBirthday();
        double prob = cb.simulate(10000, 24);

        System.out.println("Probability ="+ prob);

    }

}


Date class


import java.util.Scanner;

public class Date
{
    private String month;
    private int day;
    private int year; //a four digit number.

    public Date( )
    {
        month = "January";
        day = 1;
        year = 1000;
    }

    public Date(int monthInt, int day, int year)
    {
        setDate(monthInt, day, year);
    }

    public Date(String monthString, int day, int year)
    {
        setDate(monthString, day, year);
    }

    public Date(int year)
    {
        setDate(1, 1, year);
    }

    public Date(Date aDate)
    {
        if (aDate == null)//Not a real date.
        {
             System.out.println("Fatal Error.");
             System.exit(0);
        }

        month = aDate.month;
        day = aDate.day;
        year = aDate.year;
    }

    private boolean isLeapYear()
    {
        if (year % 100 != 0)
        {
            // For non-century years, just test for divisibility by 4
            if (year % 4 == 0) return true;
            return false;
        }
        if (year % 400 == 0)return true;
        return false;
    }
    public int daysInMonth()
    {
        switch(getMonth())
        {
        case 2: // February
            if (isLeapYear()) return 29;
            return 28;
        case 4: // April
        case 6: // June
        case 9: // September
        case 11: // November
            return 30;
        default:
            return 31;
        }
    }

    public void setDate(int monthInt, int day, int year)
    {
        if (dateOK(monthInt, day, year))
        {
            this.month = monthString(monthInt);
            this.day = day;
            this.year = year;
        }
        else
        {
            System.out.println("Fatal Error");
            System.exit(0);
        }
    }

    public void setDate(String monthString, int day, int year)
    {
        if (dateOK(monthString, day, year))
        {
            this.month = monthString;
            this.day = day;
            this.year = year;
        }
        else
        {
            System.out.println("Fatal Error");
            System.exit(0);
        }
    }

    public void setDate(int year)
    {
        setDate(1, 1, year);
    }

    public void setYear(int year)
    {
        if ( (year < 1000) || (year > 9999) )
        {
            System.out.println("Fatal Error");
            System.exit(0);
        }
        else
            this.year = year;
    }
    public void setMonth(int monthNumber)
    {
        if ((monthNumber <= 0) || (monthNumber > 12))
        {
            System.out.println("Fatal Error");
            System.exit(0);
        }
        else
            month = monthString(monthNumber);
    }

    public void setDay(int day)
    {
        if ((day <= 0) || (day > 31))
        {
            System.out.println("Fatal Error");
            System.exit(0);
        }
        else
            this.day = day;
    }

    public int getMonth( )
    {
        if (month.equals("January"))
            return 1;
        else if (month.equals("February"))
            return 2;
        else if (month.equalsIgnoreCase("March"))
            return 3;
        else if (month.equalsIgnoreCase("April"))
            return 4;
        else if (month.equalsIgnoreCase("May"))
            return 5;
        else if (month.equals("June"))
            return 6;
        else if (month.equalsIgnoreCase("July"))
            return 7;
        else if (month.equalsIgnoreCase("August"))
            return 8;
        else if (month.equalsIgnoreCase("September"))
            return 9;
        else if (month.equalsIgnoreCase("October"))
            return 10;
        else if (month.equals("November"))
            return 11;
        else if (month.equals("December"))
            return 12;
        else
        {
            System.out.println("Fatal Error");
            System.exit(0);
            return 0; //Needed to keep the compiler happy
        }
    }

    public int getDay( )
    {
        return day;
    }

    public int getYear( )
    {
        return year;
    }

    public String toString( )
    {
        return (month + " " + day + ", " + year);
    }

    public boolean equals(Date otherDate)
    {
        return ( (month.equals(otherDate.month))
                  && (day == otherDate.day) && (year == otherDate.year) );
    }

    public boolean precedes(Date otherDate)
    {
        return ( (year < otherDate.year) ||
           (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
           (year == otherDate.year && month.equals(otherDate.month)
                                         && day < otherDate.day) );
    }

    public void readInput( )
    {
        boolean tryAgain = true;
        Scanner keyboard = new Scanner(System.in);
        while (tryAgain)
        {
            System.out.println("Enter month, day, and year.");
              System.out.println("Do not use a comma.");
            String monthInput = keyboard.next( );
            int dayInput = keyboard.nextInt( );
            int yearInput = keyboard.nextInt( );
            if (dateOK(monthInput, dayInput, yearInput) )
            {
                setDate(monthInput, dayInput, yearInput);
                tryAgain = false;
            }
            else
                System.out.println("Illegal date. Reenter input.");
         }
    }

    private boolean dateOK(int monthInt, int dayInt, int yearInt)
    {
        return ( (monthInt >= 1) && (monthInt <= 12) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    }

    private boolean dateOK(String monthString, int dayInt, int yearInt)
    {
        return ( monthOK(monthString) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    }

    private boolean monthOK(String month)
    {
        return (month.equals("January") || month.equals("February") ||
                month.equals("March") || month.equals("April") ||
                month.equals("May") || month.equals("June") ||
                month.equals("July") || month.equals("August") ||
                month.equals("September") || month.equals("October") ||
                month.equals("November") || month.equals("December") );
    }

    private String monthString(int monthNumber)
    {
        switch (monthNumber)
        {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            System.out.println("Fatal Error");
            System.exit(0);
            return "Error"; //to keep the compiler happy
        }
    }
}



Last Modified: September 29, 2013 6:58 PM