leap year without date objects

I need to check to see how many leap years are between two user-inputted years and I can't use date objects. I figure I need to start with a for loop, but I get lost when to by the mod 4, mod 100, or mod 400. Any help?

for(int l=cYear; l <= eYear; l++){

if(l%4 == 0){

if(l%100 == 0){

}

}

}

also, if anyone is bored and really wants to give of themselves, I could use some more help with a pretty challenging-for-my-level program. Let me know.

Message was edited by:

kaluga

[826 byte] By [kalugaa] at [2007-11-26 19:33:30]
# 1

how about this? I need to keep track of how many leap years within this time span, and then multiply that by 366.

for(int l=cYear; l <= eYear; l++){

if(l%4 == 0){

if(l%100 == 0){

if(l%400 == 0){

leapYears++;

}//3rd if

}//2nd if

else

leapYears++;

}//3rd if

}//big for

P.S. I am sorry about indenting, something is off with it and it looks way off

kalugaa at 2007-7-9 22:05:56 > top of Java-index,Java Essentials,New To Java...
# 2

First, never use the letter "l" as a variable name. It looks too much like then number 1.

What exactly is the trouble? You'll probably want to use some combination of && and ||, but I guess you could just go with the nested if/elses.

Can you explain the rule in English or pseudocode?

jverda at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 3
Leap year occurs on years divisible by 4 but not divisible by 100. However, if a year is divisible by 100 and 400, it is a leap year. Thus, 1900, 2100, and 2200 are not leap years, but 2000 is.
kalugaa at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 4

> how about this?

What's your question?

Does it compile?

Does it run?

Does it produce the correct results?

Personally, I'd create a separate method to test if a single year is a leap year and return the appropriate boolean value. Then I'd call that method in a loop, incremementing a counter each tme it returned true.

jverda at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 5

> Leap year occurs on years divisible by 4 but not

> divisible by 100. However, if a year is divisible by

> 100 and 400, it is a leap year. Thus, 1900, 2100,

> and 2200 are not leap years, but 2000 is.

Okay, so, taking a step toward Java-izing it...

If the year is divisible by 4 and not divisible by 100

OR

the year is divisible by 400

then it's a leap year.

(Divisible by 100 and 400 is the same as divisible by 400, by the way.)

jverda at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 6
we can't use methods; my problem is producing the correct resultMessage was edited by: kaluga
kalugaa at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 7

> we can't use methods;

What? That's stupid. Are you sure your instructor said "Don't write any methods," as opposed to "Don't use any of the existing methods"?

In any case, you don't have to write a method. You can just put what would go in the method right into your main (which is itself a method).

> my problem is producing the

> correct result

What exact problem are you having?

jverda at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 8

yeah, he said like 20 times "one big main method, no other methods"

Problems: 1. I am having trouble wrapping my mind around the logic, 2. I don't know if this (below) syntax works. btw jverd, I appreciate your help

for(int i=cYear; i <= eYear; i++){

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

leapYears ++;

}

kalugaa at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 9

You don't know if it works? Well, find out.for (int i=cYear; i <= eYear; i++){

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

System.out.println(i + " is a leap year");

leapYears ++;

}

Check the output of that code and see if it's right.

DrClapa at 2007-7-9 22:05:57 > top of Java-index,Java Essentials,New To Java...
# 10
> ... > Problems: 1. I am having trouble wrapping my mind> around the logic, You seem to be doing fine!> 2. I don't know if this (below)> syntax works. > ...Well, why don't you try to compile and trun the code?
prometheuzza at 2007-7-9 22:05:58 > top of Java-index,Java Essentials,New To Java...
# 11

> yeah, he said like 20 times "one big main method, no

> other methods"

He's a ***** and should not be teaching CS or Java.

> Problems: 1. I am having trouble wrapping my mind

> around the logic,

Meaning what? You described it--or was that just copy/paste of what's in the asisgnent? I gave you some pseudocode that's practically Java already.

Just try it. Take your best shot and write some test cases and see how close you are.

> 2. I don't know if this (below)

> syntax works.

Try it. If the syntax is wrong, it won't compile.

Once you get it to compile, then test it. Here's one reason why you'd want a separate method--you could test just that method by itself. As it stands, the easiest way to test it is to always give it just one year and see if you end up with the proper count--zero or one. If you give it many, you could end up with the right number by having the same number of false positives and false negatives.

Once you know it works for one year, feed it a few lists of 5 or 10.

> btw jverd, I appreciate your help

You're quite welcome. It looks like you're on the right track. Just don't be afraid to try stuff and see what happens, and then come back with specific questions about the specific problems you observed. :-)

- Edit -

Or do like DrClap suggested and print each one. This way you can still test many at once.

Note to self: Duh, Jeff.

- - -

Message was edited by:

jverd

jverda at 2007-7-9 22:05:58 > top of Java-index,Java Essentials,New To Java...
# 12

yeah, from what I understand, it is much easier to use methods. yes, I understand the logic, and I ran the code and it seemed to work fine; I used 2001 as eYear and 1999 as cYear and leapYears came out to be 1 after the loop ran through everything. I just wanted to show it to you so you could notice any errors I didn't

kalugaa at 2007-7-9 22:05:58 > top of Java-index,Java Essentials,New To Java...
# 13

> wanted to show it to you so you could notice any

> errors I didn't

A better way would be to try it with several different years--particularly ones that could be troublesome or "special" in the logic. (But, again, observing the results of each year individually.)

1800

1900

2000

2100

2200

1901

1902

1903

1904

2001

2002

2003

2004

0

1

2

3

4

5

99

100

200

300

400

... for example, depending on how comfortable you are that, "if it handles this year correctly, that accounts for this case..."

jverda at 2007-7-9 22:05:58 > top of Java-index,Java Essentials,New To Java...
# 14

hmm, I see

well, I guess another question I have is how I can figure out how to count the non leap years and take them into account. Here is some of the problem we were given:

(1)Prompt the user for a premium amount (in dollars and cents, but assume the user does not enter the $ sign), a start date and an end date for a policy term, and an effective date for a policy cancellation. Dates should be entered with three separate prompts for year, month, and day, in that order. Month will be a number between 1 and 12, and year will have 4 digits. Assume any pair of dates is possible, between January 1, 1900 and December 31, 2200. You may assume that the user always enters a valid date. You may also assume that the user enters a start date that is before the end date. Assume the cancellation effective date is between the start and end dates. You may also assume that the premium amount the user enters is a valid number.

(2)Calculate the refund amount for the policy cancellation as follows:

a.Calculate the number of days between the cancellation date and the end date.

b.Calculate the number of days between the start and end dates.

c.Divide the result from part a by the result from part b.

d.Multiply the result from part c by the premium. This is the refund.

kalugaa at 2007-7-9 22:05:58 > top of Java-index,Java Essentials,New To Java...
# 15

> hmm, I see

>

> well, I guess another question I have is how I can

> figure out how to count the non leap years and take

> them into account.

Well, you know the total number of years, and you have now determined the number of leap years. You don't need to "count" the non-leaps like you did the leaps.

jverda at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 16
Jebus, he wants you to do all that in one big method? Why? Is he sadistic? Retarded?
jverda at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 17

> > well, I guess another question I have is how I can

> > figure out how to count the non leap years and

> take

> them into account.

yeah, after that post I realized that I just need to count up the total number and less the leapYears value from that, right?

Yeah, I don't know why our teacher is like that. Personally, this assignment is kicking me in the pants. It seems a little beyond the reach of an intro class, but I guess that is how you learn, right?

kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 18

Also, from what I read there, you don't care about the number of leap and non-leap years. Trying to do it by number of leap years will probably make it more complicated than it needs to be.

For instance, 3/1/2000 -- 2/28/2004 includes parts of 2 leap years, there won't be any Feb. 29 in there, so trying to add 2 extra days--that is, two years of 366 days--will give the wrong results.

Plus, this suggests you're using a "365 * number of non-leaps plus 366 * number of leaps," but doing number of years * days per year will make for some ugly fraction-of-a-year calculations that you'll need if you want to end up wth total number of days.

I guess if you only use that for all the years whose 1/1 to 12/31 are all included, and then tack on the partial years at the beginning and end...

jverda at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 19
> yeah, after that post I realized that I just need to> count up the total number and less the leapYears> value from that, right?Try it and see if it works. :-)
jverda at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 20

> Also, from what I read there, you don't care about

> the number of leap and non-leap years. Trying

> to do it by number of leap years will probably make

> it more complicated than it needs to be.

>

> For instance, 3/1/2000 -- 2/28/2004 includes parts of

> 2 leap years, there won't be any Feb. 29 in there, so

> trying to add 2 extra days--that is, two years of 366

> days--will give the wrong results.

>

> Plus, this suggests you're using a "365 * number of

> non-leaps plus 366 * number of leaps," but doing

> number of years * days per year will make for some

> ugly fraction-of-a-year calculations that you'll need

> if you want to end up wth total number of days.

>

> I guess if you only use that for all the years whose

> 1/1 to 12/31 are all included, and then tack on the

> partial years at the beginning and end...

dang, well, if I don't do it the way I was thinking, I seriously cannot realize another way to do it; I see what you are saying about the 3/1/2000 -- 2/28/2004 thing, though. wow . . . yeah, I was going for a 365 * number of non-leaps plus 366 * number of leaps approach. this is so hard!

kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 21
well, I have to run; could I bounce some more code/logic off you in the next couple of days before Feb. 28 at 12 (a purely "arbitrary" time . . . hee hee)? do you feel comfortable about giving out your email? if not, we can just meet here
kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 22

Just post whatever questions you have here. There are a lot of people besides me who can look at it. And no, I won't give out my email address. I get enough spam as it is, and private communications negates some of the benefits of a public forum--namely you getting input from more than one person, and others being able to learn from the discussion.

It's a tricky problem. Good luck.

Did I post these links yet? (Too lazy to go back and look.)

[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]

[url=http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html]Working in Java time: Learn the basics of calculating elapsed time in Java[/url]

[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]

[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]

jverda at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 23
cool, I can respect that. I hate spam, too. I appreciate your help and advice.
kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 24

hey this is an emergency call!! anyone, please read the above posts and let me know what I can do! I am really struggling to come up with a way to calculate the number of days as described in my earlier post.

HERE IS THE PROBLEM:

(1) Prompt the user for a premium amount (in dollars and cents, but assume the user does not enter the $ sign), a start date and an end date for a policy term, and an effective date for a policy cancellation. Dates should be entered with three separate prompts for year, month, and day, in that order. Month will be a number between 1 and 12, and year will have 4 digits. Assume any pair of dates is possible, between January 1, 1900 and December 31, 2200. You may assume that the user always enters a valid date. You may also assume that the user enters a start date that is before the end date. Assume the cancellation effective date is between the start and end dates. You may also assume that the premium amount the user enters is a valid number.

(2) Calculate the refund amount for the policy cancellation as follows:

a. Calculate the number of days between the cancellation date and the end date.

b. Calculate the number of days between the start and end dates.

c. Divide the result from part a by the result from part b.

d. Multiply the result from part c by the premium. This is the refund.

kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 25
please, i don't mean to be whiny, but i have spent a while coming up with dead ends. i would appreciate any step in the right direction
kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 26
> please, i don't mean to be whiny, but i have spent a> while coming up with dead ends. i would appreciate> any step in the right directionWell, post what you have come up with thusfar.
prometheuzza at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 27

it's kinda gay, I am even ashamed to post it, but here is what I have so far, complete with commented code that I realized isn't getting me anywhere:

Double.parseDouble(pAmount);

//start date input

int sYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting year:"));

int sMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting month:"));

int sDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting day:"));

//end date input

int eYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending year:"));

int eMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending month:"));

int eDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending day:"));

//cancel date input

int cYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel year:"));

int cMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel month:"));

int cDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel day:"));

int endCancelYearDiff = eYear-cYear;

int endStartYearDiff = eYear-sYear;

int leapYears = 0;

//START getting the End-Cancel years diff . . .ahh, what is going on here?!; still need to get the end and start year diff

for(int i=cYear; i <= eYear; i++){

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

leapYears ++;

}//if

}//for

int nonLeapYears = endCancelYearDiff - leapYears;

int totalYears = (nonLeapYears * 365) + (leapYears * 366);

//END figuring the years

//START figuring diff between End and Cancel month days

int endCancelMonthDiff = 0;

endCancelMonthDiff = eMonth - cMonth;

for (i)

//0 would be Jan, diff of 1 would mean end month is Feb.

/*switch (endCancelMonthDiff){

case 0: endCancelMonthDiff = 0;

case 1: endCancelMonthDiff = 31;

case 2: endCancelMonthDiff = 59;

case 3: endCancelMonthDiff = 90;

case 4: endCancelMonthDiff = 0;

case 5: endCancelMonthDiff = 0;

case 6: endCancelMonthDiff = 0;

case 7: endCancelMonthDiff = 0;

case 8: endCancelMonthDiff = 0;

case 9: endCancelMonthDiff = 0;

case 10: endCancelMonthDiff = 0;

case 11: endCancelMonthDiff = 0;

}*/

kalugaa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...
# 28
How can code be 'gay'? Does it accept incoming connections on outgoing ports? Actually, no, don't answer that.
DavidKNa at 2007-7-21 17:37:25 > top of Java-index,Java Essentials,New To Java...