programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Thanksgiving finder

N. Hart
Greenhorn
Posts: 5
I am trying to write a method int day(int year) that takes a year > 1999 and returns the day of the month of November on which USA Thanksgiving falls, given that 2000/11/01 falls on a Wednesday, taking into account leap years.

My work:

1) I have the sequence for the numbers, but I cannot generalise it.

We have the numbers 22-28. We start with 23, the first number in the sequence. Now we continually descend, but every 4, we descend by 2. When I say descend, I mean within the numbers, so 22\to28 or if it's the fourth, 22\to27.

Sequence: 23, 22, 28, 27, 25, 24, 23, 22, 27, 26, 25, 24, 22, 28, 27, 26, 24, 23, 22, 28, 26, 25, 24, 23, 28, 27, 26, 25, 23, 22, 28, 27, 25, 24, 23, 22, 27, 26, 25, 24, 22 ...

The pattern is not hard to see at all. How can I generalise this, how long before the cycle repeats etc.?

2) How do I create a list of numbers (array?) in Java from where I can select numbers in order? I plan to use the formula from the previous question in a list. The program in question asks for a year as input -- I will take that year mod X (where X is the number of elements in each cycle) and refer to the 'list' for the appropriate number. 23 corresponds to 2000 in my program. If 2000 is given, I want 23. If 2039 is given, I want 24 (I think?), the last number in the above sequence. This repeats for 1000 + years. If 2490 is given, I take it mod X (where X is the number of elements in the sequence before the WHOLE THING repeats), and then that answer I refer to in the list. So I need a way to number the list.

Tim Moores
Saloon Keeper
Posts: 4035
94
I don't follow your description (like what leap years have to do with it), but Thanksgiving falls on the 4th Thursday in November, so using the Java date APIs it shouldn't be too hard to figure it out for any given year, no?

N. Hart
Greenhorn
Posts: 5
Tim Moores wrote:I don't follow your description (like what leap years have to do with it), but Thanksgiving falls on the 4th Thursday in November, so using the Java date APIs it shouldn't be too hard to figure it out for any given year, no?

Well a few things. 1) I want to use modular arithmetic and maybe some loop commands, and nothing that trivializes the problem. 2) I'm almost BRAND new to programming, so I have no idea how to start.

Carey Brown
Saloon Keeper
Posts: 3329
46
If you're going to re-invent the wheel you'll need an algorithm, specifically for leap years and day of week.

N. Hart
Greenhorn
Posts: 5
Carey Brown wrote:If you're going to re-invent the wheel you'll need an algorithm, specifically for leap years and day of week.

Well I have the pattern, how would I write an algorithm to implement this?

Paul Clapham
Sheriff
Posts: 22835
43
N. Hart wrote:Well I have the pattern, how would I write an algorithm to implement this?

It looks like you already have the basics of an algorithm:
2) The program in question asks for a year as input -- I will take that year mod X (where X is the number of elements in each cycle) and refer to the 'list' for the appropriate number. 23 corresponds to 2000 in my program. If 2000 is given, I want 23. If 2039 is given, I want 24 (I think?), the last number in the above sequence. This repeats for 1000 + years. If 2490 is given, I take it mod X (where X is the number of elements in the sequence before the WHOLE THING repeats), and then that answer I refer to in the list.

Looks like a good start. So why don't you give it a go and when you run into problems, post back here?

N. Hart
Greenhorn
Posts: 5
Paul Clapham wrote:
N. Hart wrote:Well I have the pattern, how would I write an algorithm to implement this?

It looks like you already have the basics of an algorithm:
2) The program in question asks for a year as input -- I will take that year mod X (where X is the number of elements in each cycle) and refer to the 'list' for the appropriate number. 23 corresponds to 2000 in my program. If 2000 is given, I want 23. If 2039 is given, I want 24 (I think?), the last number in the above sequence. This repeats for 1000 + years. If 2490 is given, I take it mod X (where X is the number of elements in the sequence before the WHOLE THING repeats), and then that answer I refer to in the list.

Looks like a good start. So why don't you give it a go and when you run into problems, post back here?

Wait but I don't know how to implement this at all, like how do I put all these numbers in an array and then refer back to that array?

Paul Clapham
Sheriff
Posts: 22835
43
If you don't know how to use arrays then reading a tutorial would be your best course of action. Somebody here might decide to spend half an hour writing one for you just for this post, but really you're better off reading a tutorial that's already been written. Here's the Oracle tutorial: Arrays.

In general to find a tutorial for topic X in Java then the keywords Java X tutorial are very likely to find one, or many.

Carey Brown
Saloon Keeper
Posts: 3329
46
Here's an outline for the code. The significant algorithm is a calculation for Julian Day. Julian day is the continuous count of days since the beginning of the Julian Period. You can find the formula for it in Wikipedia. From there you can apply your modulus to get the day of week. Then you can compute the Nth day of week in a given year and month. Thanks Giving is the fourth Thursday in the month of November.

dayOfWeek() builds on JulianDay() and nthDayInMonth builds on dayOfWeek().