This is the question:
Write a class called DayOfYear that prompts the user to enter month, day
and year as integer values, and calculates the day of a year corresponding
to a speci ed date. (For example, if month is 2, day is 29 and year is 2000
then day of the year is 60). The program should perform a validation on
entered day, month and year, and if, the date is valid, calculates the day
of the year. (For example, if year is less or equal 0 then the program prints
that the year is invalid, and etc.) The program also checks for a leap year
and adds extra day if necessary.
The only thing i can't figure out is the algorithm to use for calculating the day of the year.
So far i've managed to get this far:
int total = 0; int i; for ( i = 1 ; i < month; i++) total += days; total += day+i; int DOY = total;
If a user entered 11/11/2011, the answer should be 315, but the above algorithm gives 322.
This is algorithm for the leap year, and the switch statements for the different number of days in a month
//Algothrim to calculate leap year
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);
//Number of days in a month
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 2:
if (isLeapYear)
days = 29;
else
days = 28;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
}
Anyone know the algorithm i need?

New Topic/Question
Reply



MultiQuote




|