# Need help with Gregorian calendar

• February 14th, 2013, 08:51 PM
Rain_Maker
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.

Code Java:

/**
*
* Author : Gregory B Shavers Jr
* CSC 225 - Online
* problem 6
*/

import java.util.*;

public class problem6
{
public static void main( String [] args)
{

double year, leapYear, leapYear2;

Scanner scan = new Scanner(System.in);

System.out.print(" What is the year? ");//ask for year
year = scan.nextDouble();

leapYear = year%400;//calualates leap year
leapYear2 = year%4;  //calualates leap year

if ( leapYear == 0  )
{
System.out.println( " This is a leap year. " ); //display leap year
}

else if ( leapYear2 == 0 )

{
System.out.println ( " This is a leap year."  ); // display not leap year
}

else
{
System.out.println( " This is not 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?

Thanks Again!

--- Update ---

I'm sorry ending with 00.
• February 14th, 2013, 09:21 PM
javaiscool
Re: Need help with Gregorian calendar
This may not be what your professor wants, but:

Code :

GregorianCalendar cal = new GregorianCalendar();
boolean leapYear = calendar.isLeapYear(year); // year is your double holding the year the user input

DOC:

GregorianCalendar (Java Platform SE 7 )

--- Update ---

Also, import java.util.Calendar

--- Update ---

If you want to create a condition statement, the only thing I can think of is maybe doing year % 10; if there is a remainder, then it's not ending in zero; if there is no remainder, do it one more time; if there is a remainder, then the second digit from the right is also not a zero; if there is no remainder, then it is a zero. After doing those two tests, then do a mod 400 with the original number and see if there is a remainder or not. -- I'm not sure if there is any built in functionality to get the numbers in a double or int at specific places (or to find the length). I asked that question here once before (if there's something similar to .length() and .charAt() like for strings)... no answer. I am a newb.
• February 14th, 2013, 09:34 PM
Rain_Maker
Re: Need help with Gregorian calendar
Thanks for the response. I completely slacked on this homework assignment!!smh lol I have an A. But it just bothers me that I won't be able to keep the A lol Thanks Again!
• February 15th, 2013, 05:09 PM
Zaphod_b
Re: Need help with Gregorian calendar
Quote:

Originally Posted by Rain_Maker
...Write a program that requests a year as input and states whether it is a leap year...

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

Code :

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:

Code java:

if ((year % 400) == 0) {
// Whatever...
}
else if ((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.

Cheers!

Z
• February 15th, 2013, 06:54 PM
javaiscool
Re: Need help with Gregorian calendar
I like this approach. Thanks Z.

Quote:

Originally Posted by Zaphod_b

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

Code :

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, 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:

Code java:

if ((year % 400) == 0) {
// Whatever...
}
else if ((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.

Cheers!

Z