Results 1 to 6 of 6
  1. #1
    giraffasaurus is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Angry Need Help With Leap Year Method

    This method is driving me crazy . It returns day for all cases of Feb. 29th, when it should only return day if it is a leap year, if it is not a leap year 1 should be returned. It works fine for everything except Feb 29th.
    Java Code:
    public int checkDay (int day)
    {
        // For months with 30 days.
        if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30)
            return day;
        // For months with 31 days.
        if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
            return day;
        // For leap years.
        // If February 29th...
        if (month == 2 && day == 29)
        {
            // Check if year is a leap year.
            if ((year%4 == 0 && year%100!=0) || year%400 == 0) 
            {
                // If year is a leap year return day as 29th.
                return day; 
            }
            // If not a leap year, return day as 1st.
            else return 1;
        }
        // If Date if February 1st through 28th return day, as it is valid.
        if (month == 2 && (day >= 1 && day <= 28))
            return day;
    // Return day as 1st for all other cases.
    return 1;
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Need Help With Leap Year Method

    Have you stepped through this with a debugger, or at least added some print statements to figure out exactly what's going on? Where exactly does the program's behavior start to differ from what you expect? What specific line?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    giraffasaurus is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need Help With Leap Year Method

    Well it has a problem on line 11 of that method. Here when date1 is 2/29/2013 my toString should print 2/1/2013. It works for all other months and for instance is date1 was 2/31/2013 it will print as 2/1/2013. It's just the 29th that's a problem.

    Java Code:
    public class TestDate {
    
    	public static void main(String[] args)
    	{
    		Date2 date1 = new Date2(2, 29, 2013);
    		
    		System.out.print("The initail date is: ");
    		System.out.print(date1.toString());
    		
    		date1.setMonth(11);
    		date1.setDay(1);
    		date1.setYear(2003);;
    		
    		System.out.print("\nDate with new value is: ");
    		System.out.print(date1.toString());
    		
    		System.out.println();
    	}
    
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Need Help With Leap Year Method

    I don't have access to the Date2 class (and posting it in its entirety is probably a bad idea), so I can't run that code. An SSCCE would make it easier to help you.

    I recommend stepping through this with a debugger, or at least adding some print statements, until you narrow the problem down.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Need Help With Leap Year Method

    Your said your checkDay routine was wrong. Did you check it out directly?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Need Help With Leap Year Method

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Leap Year program
    By Southpaw in forum New To Java
    Replies: 2
    Last Post: 12-05-2012, 04:12 AM
  2. Help with Leap Year program
    By newtojavahelp in forum New To Java
    Replies: 18
    Last Post: 12-04-2012, 07:13 PM
  3. Leap Year Test: Help!
    By Zigster in forum New To Java
    Replies: 5
    Last Post: 04-23-2012, 05:02 AM
  4. Leap Year Calculator
    By Pkaay in forum New To Java
    Replies: 9
    Last Post: 10-12-2010, 10:47 PM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 03:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •