# How to make this method more efficient?

• November 11th, 2012, 01:35 PM
failingcompsci
How to make this method more efficient?
Hi,

So I'm writing a method called getThanksgiving, it's part of a much larger class but I only need advice on how to make this particular method more efficient. The getWeekDay method just returns what day of the week November 1 is on a user-inputted year.
Code java:

public String getThanksgiving(){
String a = getWeekDay(11, 1);
int offset = 0;

if(a.equals("Friday")){
offset = 7;
}
if(a.equals("Saturday")){
offset = 6;
}
if(a.equals("Sunday")){
offset = 5;
}
if(a.equals("Monday")){
offset = 4;
}
if(a.equals("Tuesday")){
offset = 3;
}
if(a.equals("Wednesday")){
offset = 2;
}
if(a.equals("Thursday")){
offset = 1;
}

int date = 21 + offset;

thanksgiving = "Thursday, November " + date;

return thanksgiving;
}

I tried rewriting it as a for loop but it's not working.
Code java:

public String getThanksgiving(){
String a = getWeekDay(11, 1);
int offset = 8;
String[] wTable = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
for(int i = 1; i < 8; i++){
if(a.equals(wTable[i - 1])){
offset --;
}
}
}

Also, the idea of offset and adding 21 is just something my teacher wants us to do. Thanks in advance!
• November 11th, 2012, 02:20 PM
curmudgeon
Re: How to make this method more efficient?
Your method appears efficient enough, in that it appears to work correctly, and certainly won't slow things down to any great extend. Perhaps you may be worried that your code is not beautiful. If so, then you can perhaps "shift" the ugliness elsewhere by using a HashMap or something similar, but then you're still going to have somewhat ugly looking code somewhere, just not the if/else blocks that you currently have.
• November 11th, 2012, 02:38 PM
failingcompsci
Re: How to make this method more efficient?
Thanks, I was just wondering if there was a way to condense 20something lines of code into something shorter. I appreciate the feedback!
• November 11th, 2012, 03:07 PM
helloworld922
Re: How to make this method more efficient?
You can try using a HashMap to store the map from the date string to integer offset. Possibly it will be faster, but the real advantage is simplifying your code.
• November 11th, 2012, 04:03 PM
Zaphod_b
Re: How to make this method more efficient?
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:

1. Find the date of the first Thursday of the month.

2. Add 21 to get the date of the fourth Thursday of the month.

3. 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:

Code :

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:

Code :

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:

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