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 4 of 4

Thread: Need explanation of the code

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need explanation of the code

    Hello ! I need help as to what the code below does for each of its statement, when dd/mm/yyyy format is enter, it should give 20 Dec 2010 is a Monday:

    public static String dayOfWeek(int dd, int mm, int yyyy) {
    			int[] monthIndices = {1,4,4,0,2,5,0,3,6,1,4,6};
    			String[] dayOfWeek = {"Saturday", "Sunday", "Monday", 
    			                      "Tuesday", "Wednesday", "Thursday", 
    			                      "Friday"};
    			if ((yyyy % 4 == 0) && !(yyyy % 100 == 0) || 
    			    (yyyy % 400 == 0)){
    				monthIndices[0] = 0;
    				monthIndices[1] = 3;
    			}
    			int yy = yyyy % 100;
    			int yyDiv4 = yy / 4;
    			int keyNumber = monthIndices[mm-1];
    			int sum = yy+yyDiv4+keyNumber+dd;
    			int adjValue = 0;
    			if (yyyy / 100 == 18)
    				adjValue = 2;
    			else 
    				if (yyyy / 100 == 20)
    					adjValue = 6;
    				else
    					if (yyyy / 100 == 21)
    					adjValue = 4;
    			int remainder = (sum + adjValue) % 7;
    			String day = dayOfWeek[remainder];
    			return day;
    			}
    		}


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: Need explanation of the code

    If you really don't know what any of the program lines do, you need to go back to basics.
    The Java™ Tutorials

    If you do understand some of it, ask a more specific question.
    How To Ask Questions The Smart Way

    db

  3. #3
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Need explanation of the code

    @db: even if you've a good knowledge of java, you need some experience to figure out that the following statement

    if ((yyyy % 4 == 0) && !(yyyy % 100 == 0) || 
    			    (yyyy % 400 == 0))

    is used to calculate if a year is a leap year.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need explanation of the code

    thanks for reply

    have doubts as to why we need to do the code as to:

    if year modules 400 is 0
    then is_leap_year
    else if year modules 100 is 0
    then not_leap_year
    else if year modules 4 is 0
    then is_leap_year

Similar Threads

  1. Forum downtime explanation
    By JavaPF in forum The Cafe
    Replies: 2
    Last Post: September 26th, 2010, 10:55 PM
  2. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 11:11 PM
  3. Test and Set Explanation
    By bananasplitkids in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 6th, 2010, 12:10 PM
  4. Explanation on Arrays NEEDED
    By lamagiapc in forum Collections and Generics
    Replies: 4
    Last Post: November 7th, 2009, 10:36 PM