Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to make this method more efficient?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    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!
    Last edited by helloworld922; November 11th, 2012 at 03:52 PM. Reason: please use [code] tags


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default 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:

    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

Similar Threads

  1. Wondering if there's a way to make this more efficient.
    By mjr in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 29th, 2012, 06:59 PM
  2. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM
  3. How to make a simple and efficient web control panel.
    By kelsmith11 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2011, 02:28 PM
  4. [SOLVED] Anyone bored and want to make a method for me?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 8th, 2010, 01:36 PM
  5. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM

Tags for this Thread