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
What do you have so far? What part of it is giving you difficulty?
Hopefully this will help you ~
First method :
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 ..~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 }
@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.
if( 0 == $year % 4 && (0 != $year % 100 || 0 == $year % 400) )
{
# Then $year is a leap year.
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks