Title: Date Problem
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Date Problem

Create a package named date for this program.

There are 3 routines for you to complete in this Date class. You need to fill in code for:

- isLeapYear
- daysInMonth
- addDays

Complete the logic where you see comments that start with
// *********** Fill in details --- with appropriate code.

Don't modify the main method unless you want to add additional "test cases" at the end of main.

When you run this program, you should paste the results in the appropriate part of the JH3_worksheet.


package date;

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

    public void setDate(int month, int day, int year)
    {
        this.month=month;
        this.day = day;
        this.year = year;
    }

    public String toString( )
    {
        return (month + "/" + day + "/" + year);
    }
    public  boolean isLeapYear()
    {
        return isLeapYear(year);
    }
    public  boolean isLeapYear(int y)
    {
        // *********** Fill in details ---
        // If the year is not evenly divisible by 100, then a leap year
        // occurs when the year is evenly divisible by 4

        // If the year is evenly divisible by 100 then it is not a leap year
        // unless the year is also divisible by 400.
    }
    public int daysInMonth()
    {
        return daysInMonth(month, year);
    }
    public int daysInMonth(int m, int y)
    {
        // *********** Fill in details ---
        // April(4), June(6), September(9), and November(11) have 30 days
        // All other days other than February have 31 days
        // For leap years, February has 29 days otherwise 28 days
    }
    public void addDays(int daysToAdd)
    {
        // *********** Fill in details ---
        // add daysToAdd and update day, month and year accordingly
    }

    public static void main(String[] args)
    {
        Date d = new Date();
        d.setDate(4,13,2010);
        d.addDays(10);
        System.out.println(d);

        d.setDate(4,13,2010);
        d.addDays(21);
        System.out.println(d);


        d.setDate(2,13,2010);
        d.addDays(65);
        System.out.println(d);


        d.setDate(2,13,2008);
        d.addDays(65);
        System.out.println(d);

        d.setDate(2,13,2000);
        d.addDays(65);
        System.out.println(d);


        d.setDate(2,13,1900);
        d.addDays(65);
        System.out.println(d);


        d.setDate(2,13,1900);
        d.addDays(420);
        System.out.println(d);

//  If you want to add additional test cases, please add then at the end of main

    }


}