The point to me is not whether the code 'needs' to be more 'efficient' but determining a way to use a table to do the calculations, as your instructor suggested rather than a brute-force bunch of if statements.
(The suggestion by helloworld922 of using a HashMap rather than looping through a table is more appealing to me than either of the above, but I would usually try it the way the instructor suggests. Sometimes instructors actually have reasons for the suggestions that they make.)
Anyhow...
The problem boils down to this:
- Find the date of the first Thursday of the month.
- Add 21 to get the date of the fourth Thursday of the month.
- Taa-daa!
Now, how do you find the date of the first Thursday if you know the weekday of the first day of the month?
Here's a list of all seven possibilities:
First day of the month is Friday ==> Date of the first Thursday is 7
First day of the month is Saturday ==> Date of the first Thursday is 6
First day of the month is Sunday ==> Date of the first Thursday is 5
First day of the month is Monday ==> Date of the first Thursday is 4
First day of the month is Tuesday ==> Date of the first Thursday is 3
First day of the month is Wednesday ==> Date of the first Thursday is 2
First day of the month is Thursday ==> Date of the first Thursday is 1
Now, the question is: How can you use your wTable with a given String for the first day of the month to calculate the date of the first Thursday?
Starting with something similar to what you seem to be trying, consider the following:
Suppose you have the String array for the weekdays as you had in your program:
String [] wTable = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
Now, Suppose you have a String named dayStr that can take on the value of any of the days of the week in the table.
You want a loop that indexes through the table and stops when it finds a match for dayStr.
Here's how the loop can work:
If dayStr is "Friday," the loop stops at index value = 0, and you know that the date of the first Thursday is 7
If dayStr is "Saturday, the loop stops at index value = 1, and and you know that the date of the first Thursday is 6
.
.
.
If dayStr is "Thursday, the loop stops at index value = 6, and and you know that the date of the first Thursday is 1
How can you get the correct date of the firstThursday?
Here's one possibility:
Start with an integer named firstThursday set equal to 7
Each time through the loop that does not find a match for dayStr, decrement firstThursday.
When you do find a match for dayStr, break out of the loop.
At that point, the date of the first Thursday is in the variable conveniently named firstThursday.
Bottom line: First think about what you really need the program to do. Write it down. In this case, maybe find calendars that show months like my example and verify that I got it right:
Examples for all seven possibilities:
November, 2012: First day of month is Thursday. Date of first Thursday is 1, Thanksgiving is Nov 22
November 2006: First day of month is Wednesday. Date of first Thursday is 2, Thanksgiving is Nov 23
November 2011: First day of month is Tuesday. Date of first Thursday is 3, Thanksgiving is Nov 24
November 2010: First day of month is Monday...
November 2009: First day of month is Sunday...
November 2008: First day of month is Saturday...
November 2002: First day of month is Friday...
Then consider how to write code to implement the calculations. I mean, once you know the date of the first Thursday, just add 21 to find Thanksgiving, right?
Bottom line: It's actually easier to do than it is to talk about how to do it. The actual loop is three or four lines of code. The important thing is to think about it before trying to do it.
Post-bottom-line comment (if that's allowed):
For this simple problem the method of looping through the table has fewer lines of code than the 'brute-force' approach, but the 'brute-force' approach is more-or-less correct "at a glance," and might be easier to defend to a reviewer with a certain mindset.
I mean, the looping-through-the table thing is more "elegant," but maybe takes a little getting used to. If you are submitting the code in a design review presided over by some code genius, one approach may be better. On the other hand, if the presiding officer is an MWUTWC (a Manager Who Used To Write Code) back in the 20th century, maybe the other would be preferred.
Cheers!
Z