4 Replies - 1615 Views - Last Post: 03 April 2012 - 07:01 PM Rate Topic: -----

#1 blackjnr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-March 11

Day of Year

Posted 03 April 2012 - 04:40 AM

Got an assignment due tomorrow, most of it is done, except i can't figure out one bit.
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?

Is This A Good Question/Topic? 0
  • +

Replies To: Day of Year

#2 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 207
  • Joined: 13-October 10

Re: Day of Year

Posted 03 April 2012 - 05:44 AM

This could help:
	private int dayOfYear(int year, int month, int day) // 0<month<=12
	{
		short[] daysInMonth = new short[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		short[] daysInMonthLeap = new short[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		
		boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);
		
		int yearDay = 0;
		for(int i=0; i<month-1; i++)
		{
			if(isLeapYear) yearDay += daysInMonthLeap[i];
			else yearDay += daysInMonth[i];
		}
		yearDay+=day;
		return yearDay;
	}

Was This Post Helpful? 1
  • +
  • -

#3 blackjnr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-March 11

Re: Day of Year

Posted 03 April 2012 - 03:16 PM

Thank you very much sir!!!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Day of Year

Posted 03 April 2012 - 06:27 PM

I would simplify it a bit :)

private int dayOfYear(int year, int month, int day) // 0<month<=12
{
	short[] daysInMonth = new short[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
             daysInMonth[1] = 29;
	
	int yearDay = 0;
	for(int i=0; i<month-1; i++)
	{
	     yearDay += daysInMonth[i];
	}
	yearDay+=day;
	return yearDay;
}


Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Day of Year

Posted 03 April 2012 - 07:00 PM

I'd simplify it a bit more :)

private int dayOfYear(int year, int month, int day) // 0<month<=12
{
	short[] monthDays = new short[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

    int yearDay = monthDays[month-1] + day;
	
	if(((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)) && month > 2)
             yearDay++;
	
	return yearDay;
}




edit: disabled leap day adjustment for jan and feb

This post has been edited by r.stiltskin: 03 April 2012 - 07:38 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1