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.

Need help with Gregorian calendar

Here is my assignment.

The Gregorian calendar was introduced in 1582. Every year divisible by four was
declared to be a leap year, with the exception of the years ending in 00 and not divisible
by 400. So 1200, 1600, and 2000 are leap years while 1300, 1400, 1800, and 1900 are
not. Write a program that requests a year as input and states whether it is a leap year.

I need help writing a condition statement to finish this assignment. My condition statement should not allow a number to be a leap year if its not equal to 00 and not divisible by 400. Is there any ideas?

Re: Need help with Gregorian calendar

...Write a program that requests a year as input and states whether it is a leap year...

You could just follow your logic to the bitter end.

Starting with what you wrote, pseudo-code for the algorithm could look something like the following:

IF (year IS DIVISIBLE BY 400) THEN
It is a leap year. // Divisible by 400 is all you need to know!
ELSE IF (year IS DIVISIBLE BY 4) THEN
IF (year IS DIVISIBLE BY 100)) THEN
It is not a leap year. // Not divisible by 400. Divisible by 100.
ELSE
It is a leap year. // Divisible by 4. Not divisible by 100
END IF
ELSE
It is not a leap year. // Not divisible by 4.
END IF

Of course there are other sequences of tests that also give the correct answer. (For example: Test for divisibility by 400, then by 100, then by 4, stopping wherever appropriate.)

More experienced programmers are always tempted to compress things to one or two compound conditionals instead of using nested conditionals. Sometimes that makes things more clear, but sometimes cleverness and "elegant" terseness makes it more difficult to understand. You should lay out the logic steps in a way that is "obviously correct" from your own point of view.

Notes:

Why the heck do you use a double precision floating point variable for something that is, by its very nature, an integer? In Java you might get the correct output with floating point variables, but, in my opinion, it's just wrong-headed. There are situations where it is appropriate to use floating point. arithmetic This is simply not one of those.

Why the heck do you use numerical variables for the result of tests of divisibility? I mean, you might be able to get the correct answer, but it's just an indication of muddy thinking (perhaps influenced by previous experience with C or some other language that doesn't have boolean data types, or the effect of getting help from someone like that).

As far as that goes, why even have variables for the results of divisibility tests at all? Wherever possible I think that statements should be self explanatory and not make a program maintainer (or person trying to debug the program) have to refer back to some previous assignment statement to see what condition is being tested:

if((year %400)==0){// Whatever...}elseif((year %4)==0){// Whatever}

Bottom line: Get into the habit of doing whatever you think you can do to make it "correct at a glance." You will thank me later.