Closed Thread
Results 1 to 7 of 7

Thread: School Project - Leap Year Program

  1. #1
    Bohican is offline Newbie
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    School Project - Leap Year Program

    Hi I have to write a program that counts how many days it has been since the beginning of the year. I have to write one using switches and one using if statements. I really need help and I'm completely lost.

    Name your programs: WhichDayWithCase.java and WhichDayWithIf.java.
    The user will enter a date by entering the month, the day, and the year. All three values will be entered as integers. You can assume the input will be a legitimate date.

    The program will then print the date followed by how many days have elapsed since the beginning of the indicated year. The program will take into account whether the year is, or is not, a leap year.

    A year is a leap year if it is divisible by 4. However, there are two exceptions. The first exception: a year divisible by 100 is not a leap year. The second exception: a year divisible by 400 is a leap year. Thus, 1800 and 1900 are not leap years, but 2000 is a leap year.

    For both programs:
    Calculate the number of days in February by using if statements to determine whether the year is, or is not, a leap year. Nested if statements can be used as needed.

    For WhichDayWithCase.java:
    Use switch statement(s) to compute the number of days in the year (other than the if statements mentioned above to compute the number of days in February).

    For WhichDayWithIf.java:
    Use if statement(s) to compute the number of days in the year. Nested if statements can be used as needed.

    Some examples:
    A January example:
    Enter the month: 1
    Enter the day: 23
    Enter the year: 2009
    Date: 1/23/2009
    The date 1/23/2009 is day number 23

    A February example:
    Enter the month: 2
    Enter the day: 17
    Enter the year: 1996
    Date: 2/17/1996
    The date 2/17/1996 is day number 48

    A pair of March examples. For these, it is necessary to know whether the year is, or is not, a leap year:
    Enter the month: 3
    Enter the day: 23
    Enter the year: 1962
    Date: 3/23/1962
    The date 3/23/1962 is day number 82

    Enter the month: 3
    Enter the day: 23
    Enter the year: 1964
    Date: 3/23/1964
    The date 3/23/1964 is day number 83

    In these next two examples, note that 1776 is a leap year, but 1775 is not.
    Enter the month: 7
    Enter the day: 4
    Enter the year: 1776
    Date: 7/4/1776
    The date 7/4/1776 is day number 186

    Enter the month: 7
    Enter the day: 4
    Enter the year: 1775
    Date: 7/4/1775
    The date 7/4/1775 is day number 185

    The year 2000 is a leap year, since years divisible by 400 are leap years.
    Enter the month: 10
    Enter the day: 13
    Enter the year: 2000
    Date: 10/13/2000
    The date 10/13/2000 is day number 287

    The year 1900 is not a leap year, since years divisible by 100, but not by 400, are not leap years.
    Enter the month: 12
    Enter the day: 25
    Enter the year: 1900
    Date: 12/25/1900
    The date 12/25/1900 is day number 359

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: School Project - Leap Year Program

    What do you have so far? What part of it is giving you difficulty?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    R3.RyozKidz Guest

    Re: School Project - Leap Year Program

    Hopefully this will help you ~

    First method :
    Code:
    if( x % 4 == 0)
    {
        if( x % 400 == 0)
        {
            //  x is a leap year
        }
        
        if( x % 100 == 0)
        {
            // x is not a leap year
        }
    }
    else
    {
        // x is not a leap year
    }
    Second way , figure out yourself because it is simplified from the first method ! All the information that you needed is all inside the question you posted including how to write a code that can determine whether a year is a leap year or not ..~

  5. #4
    Flying Dutchman's Avatar
    Flying Dutchman is offline Programming God
    Join Date
    Feb 2010
    Location
    ::1
    Posts
    821
    Rep Power
    13

    Re: School Project - Leap Year Program

    @R3.RyozKidz,

    this is partly off topic; perhaps I'm wrong but what if year is dividable (does that word even exist?) by 4 and not by 400? It still is a leap year afaik.

  6. #5
    R3.RyozKidz Guest

    Re: School Project - Leap Year Program

    Quote Originally Posted by Bohican View Post
    A year is a leap year if it is divisible by 4. However, there are two exceptions. The first exception: a year divisible by 100 is not a leap year. The second exception: a year divisible by 400 is a leap year. Thus, 1800 and 1900 are not leap years, but 2000 is a leap year.
    The code i wrote was based on this information.
    As i know there is a more simple clear cut way which i have forgotten

  7. #6
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: School Project - Leap Year Program

    Quote Originally Posted by R3.RyozKidz View Post
    Hopefully this will help you ~

    First method :
    Code:
    if( x % 4 == 0)
    {
        if( x % 400 == 0)
        {
            //  x is a leap year
        }
        
        if( x % 100 == 0)
        {
            // x is not a leap year
        }
    }
    else
    {
        // x is not a leap year
    }
    Second way , figure out yourself because it is simplified from the first method ! All the information that you needed is all inside the question you posted including how to write a code that can determine whether a year is a leap year or not ..~
    wrong... because if an x is dividable by 400, its for sure dividable by 100!
    (x= a*400) => (x= a*100*4) => (x= (a*4)*100)

    only need to change it to :
    Code:
    else if (x % 100 == 0)

  8. #7
    R3.RyozKidz Guest

    Re: School Project - Leap Year Program

    if( 0 == $year % 4 && (0 != $year % 100 || 0 == $year % 400) )
    {
    # Then $year is a leap year.
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. School project
    By siraggi in forum General Programming
    Replies: 1
    Last Post: 11-09-2010, 05:07 PM
  2. Project for school.
    By unbrknchane in forum C and C++
    Replies: 11
    Last Post: 02-18-2010, 12:03 PM
  3. Help with my school c++ project..
    By akashkingofppc in forum C and C++
    Replies: 1
    Last Post: 05-13-2009, 09:57 AM
  4. Leap Year program
    By Gz1987 in forum C and C++
    Replies: 3
    Last Post: 11-10-2007, 05:26 PM
  5. School Project
    By JLeight07 in forum The Lounge
    Replies: 2
    Last Post: 10-28-2007, 12:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

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