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

Thread: New Programming Student Looking for Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New Programming Student Looking for Help

    Hello everyone I am new to this site I have recently joined the university of Computer science in Greece and although we have never used java before our teacher in ''programming with java'' has given us some homework,that I believe, is really difficult for a beginner so I could really use your help.I am not looking for a clear out solution which I will copy paste.I am looking for someone to show me a program that could do what I want it to do and then explain me how it works and some parts that I possibly will not understand.So here is the problem.I want my program to take two dates which will be given by the user and calculate the days that have passed from the one date to the other.The first date will be today and the second one will be any date from the past.Also after it calculates the days passed it should also find what day was the past date.I need to state that I cant use anything that does the procedure automatically so it should be done with plain orders not even loops if possible just ''if'' .Thanks a lot for your help looking forward to some answers.If anything is unclear please ask me so I can clarify.Sorry if my english is bad,but I havent used them for a while.
    Looking forward to your replies,
    Nikos


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    So here is the program I have created in order to solve this.
    import acm.program.*;
    public class ask2 extends Program {
    public void run() {
    println ("Give the first date.Year,Month,Day");
    int year1 = readInt();
    int month1 = readInt();
    int day1 = readInt();
    println("Give the second date.Year,Month,Day");
    int year2 = readInt();
    int month2 = readInt();
    int day2 = readInt();
    int daydifference= 0;
    boolean logic1;
    for (int i=year2;i<year1;i++){
    logic1=(i%400==0)||((i%4==0)&&(i%100!=0));
    if (logic1) {
    daydifference=daydifference + 366;
    }
    else{
    daydifference=daydifference + 365;
    }
    }
    boolean logic2;
    boolean logic3;
    boolean logic4;
    for (int i=month2;i<month1;i++){
    logic2=(i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i= =10)||(i==12);
    logic3=(i==4)||(i==6)||(i==9)||(i==11);
    logic4= ((i==2)&&((year1%400==0)||((year1%4==0)&&(year1%10 0!=0))));
    if ((month1-month2)>0) {
    if (logic2) {
    daydifference=daydifference + 31;
    }
    else if (logic3){
    daydifference=daydifference + 30;
    }
    else if (logic4){
    daydifference=daydifference + 29;
    }
    else if (! logic4){
    daydifference=daydifference + 28;
    }
    }
    }
    for (int i=month1;i<month2;i++){
    logic2=(i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i= =10)||(i==12);
    logic3=(i==4)||(i==6)||(i==9)||(i==11);
    logic4= ((i==2)&&((year1%400==0)||((year1%4==0)&&(year1%10 0!=0))));
    if ((month1-month2)<0) {
    if (logic2) {
    daydifference=daydifference - 31;
    }
    else if (logic3){
    daydifference=daydifference - 30;
    }
    else if (logic4){
    daydifference=daydifference - 29;
    }
    else if (! logic4){
    daydifference=daydifference - 28;
    }
    }
    }
    if ((day1-day2)>0) {
    daydifference=daydifference + (day1-day2);
    }
    else {
    daydifference=daydifference - (day2-day1);
    }
    println("The days that have passed between those dates are " +daydifference);
    }
    }


    My problem is that it gives me the day difference but with 1 day less.Could anyone see the problem here?Also I could really use some help on the part of finding what day it was that date

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    I have come ro realize that possibly this problem only occurs when of the given months is december.I think that if the months given are not december it works perfectly fine

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

    Default Re: New Programming Student Looking for Help

    I think your leap year logic is not correct.

    I'll use the following notation:
    date1 = year1 month1 day1 from your program
    and
    date2 = year2 month2 day2 from your program

    Then...
    With date1 = 2012 10 30 and date2 = 2011 10 30 your program shows 365 days (Incorrect: Should be 366 since going from date2 to date1 passed through the end of February in a leap year)

    With date1 = 2013 10 30 and date2 = 2012 10 30 your program shows 366 days (Incorrect: Should be 365 since going from date2 to date1 did not pass through the end of February in a leap year.)

    (I haven't checked very many other date differences, so I can't say whether there are other problems.)



    Anyhow, if you get the day difference right, then you can calculate the day of the week for a given date as follows:

    Suppose you want day of week for some date after, say, January 1, 1900.

    Now, you can look it up: In 1900, January 1 was on Monday. This can be your reference date.

    So, get the target year, month, day in your year1, month1, day1 and calculate the number of days between Jan 1, 1900 and the target date. The day of the week can be calculated as

    int weekday = daydifference % 7;

    Where weekday ==0 means it's Monday,
    weekday == 1 means it's Tuesday,
    etc.

    Here's a check: Find out what day of the week October, 30, 2012 falls on:

    There are a number of Julian Day calculators on the web that you can use to check your work (and mine).


    According to the calculator I used, the number of days from Jan 1, 1900 to October 30, 2012 is 41210.

    Then we can calculate 41210 % 7 = 1.

    Therefore October 30, 2012 is on Tuesday. (Whew.)

    Now, didn't your original post say that you were supposed to do it without loops?

    Well, there is a way that just uses integer arithmetic. No loops, no arrays of monthdays. Heck, it doesn't even use "if" statements or other logic. Just math. Really. Just math.

    Look at the calculations here: Julian Day - Wikipedia

    The first JDN formula (about halfway down the page) is what you want. The formulas can be implemented directly with a grand total of four lines of Java.

    You can calculate that for October 30, 2012, JDN1 = 2456231
    You can calculate that for January 1, 1900, JDN2 = 2415021

    Now, JDN1 - JDN2 = 41210, as I mentioned above.

    (There is also an equation that lets you calculate the weekday without needing a separate calculation for a reference date. It's built in.)

    Be sure to read the explanation of where those goofy-looking formulas come from: Explanation of Julian Day Number Calculation
    The formulas actually make sense when they are explained...


    My favorite web-based Julian Day calculator is here: Julian Day Number Calculations at nr.com. It's great for checking your work!

    Cheers!

    Z
    Last edited by Zaphod_b; October 30th, 2012 at 10:43 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    do you have any reccomendations about the part of the leap year and the part of december and such?Also I didnt really get the day calculation thing

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

    Default Re: New Programming Student Looking for Help

    Quote Originally Posted by NikosNt View Post
    do you have any reccomendations about the part of the leap year
    No.

    Quote Originally Posted by NikosNt View Post
    Also I didnt really get the day calculation thing
    Suppose we are given a reference date that falls on Monday. There are seven days in a week, right?

    Then, if there are N days from that date until the target date, and if N is divisible by 7, it's Monday again, since there are an integer number of weeks from reference to target.

    Suppose, for simplicity, there are 21 days between the reference date and the target date.

    Since 21%7 == 0 that means it's Monday. (That is, there are an integer number of weeks. So the weekday is the same.)

    Suppose there are 22 days.

    Since 22 % 7 == 1, that means it's Tuesday (Since there is a remainder of 1 after dividing by 7, that means there is one more day than an integer number of weeks.)

    Supose there are 40123 days.

    Since 40123 % 7 == 6, that means it's Sunday (Since there is a remainder of 6 after dividing by 7, that means there are six more days than an integer number of weeks.)


    Cheers!

    Z

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    what If I dont have a given date? I mean if I dont know what day it is for any day

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

    Default Re: New Programming Student Looking for Help

    I already told you one that you can use: Jan 1, 1900 is Monday. If you would rather have some other date, then look it up on one of the many calendar programs that you can find on the web.

    Cheers!

    Z

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    I cannot use any date I want there are some restrictions.I actually cant use any date

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

    Default Re: New Programming Student Looking for Help

    So, what date can you use for a reference? I mean you have to start somewhere.

    What, exactly, are these restrictions?

    Cheers!

    Z

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    we can ask the user to say what name is the first date of the present but he said it's not necessary and had to allow us to do it since lots of people asked for it

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

    Default Re: New Programming Student Looking for Help

    What is the earliest date that your program must handle? You have to know this. Period.

    Is it Jan 1, 4000000 BCE? (Western Christian countries used to call it B.C., so this is 4 million years B.C.)

    Is it Jan 1, 32 CE? (Western Christian countries used to call it A.D., so this is 32 A.D.)


    What?

    That's what I meant to ask, since you said that there are some "restrictions" and you can't use "any date." What dates can you use?

    Reference Calendar - Wikipedia



    Cheers!

    Z
    Last edited by Zaphod_b; October 31st, 2012 at 02:47 PM.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    the earlierst date is the first day of the first year so propably 1/1/1

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

    Default Re: New Programming Student Looking for Help

    Quote Originally Posted by NikosNt View Post
    the earlierst date is the first day of the first year so propably 1/1/1
    Using the formula that I referenced way back in my first post, that date fell on Monday.
    There is no way (really, No Way) that a program could discover or calculate that without some reference. You as the programmer know that (now that I have told you), and you can make your code act accordingly.

    Now, because of things that happened to human-made calendars, in my opinion, there is no practical way to start at 1 1 1 and count days and months and years to get to a modern date.

    I didn't say no way; I gave an opinion that there is no practical way for a beginning programmer (or maybe even a not-so-beginning programmer) to implement a counting-style program, in a reasonable amount of time, to do the deed. It's only an opinion, and if you want to try it, well, that's up to you.

    I mean, if you had said that Jan 1, 1900 was the earliest you had to handle, then, maybe, counting days and months and years to some later date might be practical, but not from year 1. At least that's the way I see it.

    On the other hand, the formulas that I previously referenced involve a grand total of as few as four lines of Java integer arithmetic. (But I said that already.)

    I call your attention to something that happened in 1582 that affects the modern calendar. It is in one of the Wikipedia references that I gave. (Or you can find other references to Gregorian Calendar on the web.) Because of this human quirk that adjusted calendars beginning in the 16th century, I personally feel that trying to play with dates earlier than 1600 is rather challenging. Even as late as the beginning of the 18th century not all countries had changed from the old calendar to the new one, so different countries' calendars have interesting holes with "missing days" sprinkled throughout history. (Non-Catholic countries, in particular, resisted being influenced by anything coming from Catholic Pope Gregory XIII, and the English didn't get around to modernizing their calendar until the beginning of 1752.)

    On an (almost) unrelated note, there were "Old Style" dates that considered new years beginning in March, whereas the "New Style" dates consider that new years begin in January. Try to figure out the actual day of the week that some first-half 18th century historical figure, say George Washington, was born. You have to take into account Old Style and New Style and Gregorian and Julian calendar issues. It's fun if you like that kind of stuff, but doesn't help you get your program running.

    Reference: Old Style and New Style dates - Wikipedia


    Bottom line: If you use the formulas, you can calculate the JDN (Julian Date Number) for any date from Jan 1, 4713 BCE until sometime in the year 3768 CE.

    Then the day of the week is calculated by JDN % 7.

    If the result is 0, it's Monday.
    If the result is 1, it's Tuesday.
    etc.


    Cheers!

    Z
    Last edited by Zaphod_b; October 31st, 2012 at 04:57 PM.

  15. #15
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Programming Student Looking for Help

    thanks a lot for your effort to help me (even though I didnt like a part of the last message).
    Cheers
    Nikos

Similar Threads

  1. New Programming Student, Having Trouble With Abstract Classes/Interfaces
    By bulx0001 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 10th, 2012, 07:24 AM
  2. New student needing help!
    By newstudent in forum Object Oriented Programming
    Replies: 2
    Last Post: January 30th, 2012, 12:49 AM
  3. Hello to all!/Java Student.
    By Benjamin Cainan in forum Member Introductions
    Replies: 1
    Last Post: May 3rd, 2011, 09:48 AM
  4. Student Programs
    By Suzanne42 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 26th, 2011, 09:20 AM
  5. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM