[VIEWED 20806
TIMES]
|
SAVE! for ease of future access.
|
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 6:38
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Any serious Java people in here? For some reasons, the following code doesn't work for leap year. Any help/suggestion is appreciated. The classes are Date and TestDate: public void addMonth(int m) { month = month + m; if (month>12) { month-=12; addYear(1); } } public boolean isLeap(int y) { if (y%100==0) { if (y%400==0) return true; else return false; } else if (y%4==0) return true; return false; } public void addDay(int d) { day = day + 1; if (day>28) { if (month==2) { if (isLeap(year)) { if (day>29) { day-=29; addMonth(1); return; } else return; } day-=28; addMonth(1); } else if ((month==4||month==6||month==9||month==11)&&day>30) { day-=30; addMonth(1); } else if (day>31) { day-=31; addMonth(1); } } } public void addYear(int y) { { year += y; if (month==2) if (day==29) if (!isLeap(year)) { setMonth(3); setDay(1); } } } public String toString() { String d=""+day,m=""+month,y=""+year; if (d.length()<2) d="0"+d; if (m.length()<2) m="0"+m; while (y.length()<4) y="0"+y; return m + "/"+ d + "/" + y; //return the value of } //concatenated month, day, and year }
|
|
|
|
SAJHACOP
Please log in to subscribe to SAJHACOP's postings.
Posted on 09-28-06 8:44
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
public boolean isLeap(int y){ if(y%4==0) return true; else return false; } ************ public void addDay(int d) { day=d+1; if(day > 28) { if((month==2)&&(isLeap(year))){ day-=28; addMonth(1); } else if((month==2)&&(!isLeap(year))){ day-=28; addMonth(1); } ... Leap year is always divisible by 4. I don't know why you are using y%400 and y%100.
|
|
|
sajhauser
Please log in to subscribe to sajhauser's postings.
Posted on 09-28-06 8:47
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Slackdemic, the definition of leap year is wrong in your isLeap(int y) method. 1. Every year divisible by 4 is a leap year. 2. But every year divisible by 100 is NOT a leap year 3. Unless the year is also divisible by 400, then it is still a leap year.
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 8:52
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Yes, I also got that from this website, too. Thanks! www.timeanddate.com/date/leapyear.html
|
|
|
Prem Charo
Please log in to subscribe to Prem Charo's postings.
Posted on 09-28-06 8:54
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Don't worry. Help is on the way. I just dialed 991.
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 8:59
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Didn't mean to be rude earlier, guys, but what I did is good: Every year divisible by 4 is a leap year. But every year divisible by 100 is NOT a leap year Unless the year is also divisible by 400, then it is still a leap year. The year that is divisible by 100 is not leap year UNLESS it is divisible by 400! See there? That's exactly what I did. See the if loop: Posted on 09-28-06 6:38 PM Reply | Notify Admin ny serious Java people in here? For some reasons, the following code doesn't work for leap year. Any help/suggestion is appreciated. The classes are Date and TestDate: public void addMonth(int m) { month = month + m; if (month>12) { month-=12; addYear(1); } } public boolean isLeap(int y) { if (y%100==0) { if (y%400==0) return true; -----------> means, if it is divisible by 100 AND 400, return true else return false; ------>if it is divisible ONLY by 100....return false.. } else if (y%4==0) return true; return false; }
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 9:01
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
If you are really trying to figure it out, I will say you, have quite a sense of humor. If you are just playing, stop it, I am stressed!
|
|
|
sajhauser
Please log in to subscribe to sajhauser's postings.
Posted on 09-28-06 9:01
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
but you haven't included all conditions... everything should be inside one if condition..in nested form starting with yr%4
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 9:01
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
And that message is for (somebody's) lovebird.
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 9:06
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Sajhauser: I think there are two conditions for the leap year: One, the year that is divisible by 4 (which is at last). Two, the year that is divisible by 100 AND (then) 400. I don't think they have to be within one pair of braces...or maybe I can make it that way, but I don't think that is the problem...!
|
|
|
sajhauser
Please log in to subscribe to sajhauser's postings.
Posted on 09-28-06 9:08
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
argggg.... check out here http://users.dickinson.edu/~braught/courses/cs132f01/classes/code/LeapYear.src.html you haven't really followed the definition kyaaaaaa!
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 9:16
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hyatteri! that is just the definition. I think I have "really" followed the dang definition. I know how to google, too, Sajhauser (:P) Anyway, thanks, let me play with it...I am kind of afraid to post all my codes for you all so you would know what exactly I am talking...This is one of the big assignments that is due tomorrow, and I assume there are other guys from my class that know how to google like you and I. :P
|
|
|
lootekukur
Please log in to subscribe to lootekukur's postings.
Posted on 09-28-06 9:25
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
{ f (y%100==0) { if (y%400==0) return true; -----------> means, if it is divisible by 100 AND 400, return true else return false; ------>if it is divisible ONLY by 100....return false.. } else if (y%4==0) return true; return false; } --well , as per my reading, you should have following conditions 1) the year is divisible by 4 (NOT the sufficient condition, so you have to look into) (a) is the year divisible by 4 and not by 100? if yes, it's a leap year else not (b) is the year divisible by 4 and by 100? (NOT the sufficient condition, so you have to look into) (i)is the year also divisible by 400 as well? if yes, it's a leap year, else not obviously, it's not a leap yr if its not divisible by 4. sajhauser is right and she provided the link as well. you should have gone through it :P LooTe
|
|
|
sajhauser
Please log in to subscribe to sajhauser's postings.
Posted on 09-28-06 9:27
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
you haven't applied the dang logic behind the definition! you have made assumption as if there are only 2 condition, when, infact there are 3..so you have to first check by 4. the definition first tells you to check with 4, right? // Is theYear Divisible by 4? if (theYear % 4 == 0) { // Is theYear Divisible by 4 but not 100? if (theYear % 100 != 0) { System.out.println(theYear + " is a leap year."); } // Is theYear Divisible by 4 and 100 and 400? else if (theYear % 400 == 0) { System.out.println(theYear + " is a leap year."); } // It is Divisible by 4 and 100 but not 400! else { System.out.println(theYear + " is not a leap year."); } } // It is not divisible by 4. else { System.out.println(theYear + " is not a leap year."); }
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 9:48
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
You are right guys! I have been misplacing 400, 100, and 4. The program is whole lot more than that, but Thanks!!! You all rock!....Wish me good luck!
|
|
|
sajhauser
Please log in to subscribe to sajhauser's postings.
Posted on 09-28-06 10:25
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Good luck! and don't pre-judge me okay!
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-28-06 10:42
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
I was not "pre-judging you, Sajhauser...My problem was actually the wrong output which didn't have much connection with the right or wrong definition of leap year. And, I was stressed. It was my understanding that was not correct. Maybe, I am little slow. :P Maybe, really, at times. The leap year is something that is divisible by 4 BUT NOT divisible by 100. But anything that is divisible 100 and 400 is still leap year. There are three conditions there: 1. Anything divisible by 4 is leap year. 2. BUT if it is not leap year if it is divisible by 100. 3. It is leap year if it is divisible by 100 and 400. This is what I did and it went well. { /*if(y % 4 ==0 ) { // divisible by 4 if(y % 400 ==0) //leap year is { if(y %100 ==0) { return false; } return true; } return true; } return false; }*/ Coincidentally, I get the right output, too. And, I am done! Thank you!
|
|
|
lootekukur
Please log in to subscribe to lootekukur's postings.
Posted on 09-28-06 10:58
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|