This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Leap year program

 
Emma Elliott
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a Java program using JCreator that will determine if a year is a leap year. Since the introduction of the Gregorian calender on October 15, 1582, a year is called a leap year if the following applies:
- it is divisible by 4 (e.g. 1980) ie year % 4 == 0;
- it is not divisible by 100 (e.g. 1900);
- but it is divisible y 400 (e.g. 2000).

I need my program to ask the user to input a year and determine, using an if statement, if that year is a leap year. Tne program should output a suitable sentence using System.out.print stating whether or not the year input is a leap year.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so let's see what you've got so far.
 
Emma Elliott
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class LeapYear
{
public static boolean isLeapYear(int year)
{

if( year % 4 != 0 )
return false;
// yrs NOT divisible by 4 are not LY

if( year <= 1582 )
return true;
// yrs div. by 4, before (including) 1582, are LY

if( year % 400 == 0 )
return true;
// yrs div. by 400, after 1582, are LY

if( year % 100 == 0 )
return false;
// century years NOT div. by 400, after 1582, are not LY

return true;
// other div.-by-4 years are LY
}

public static void main (String[] args)
{
int y;
while(true){
System.out.println("Enter the year: ");
y = Stdin.getInt();
if( y < 0 ) break;
System.out.print ("Is it a leap year? ");
System.out.println( isLeapYear(y) );
}
}
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emma,

your leap year method is OK.
Your main method also looks OK, but with one little thing that has to be done:
your local int variable y is not initialised properly.
It is not sure, that y will ever get a value, so you must define it as
int y=0; // or int y=42; or whatever.
with int y; alone, it cannot compile.

I don't know what the Stdin.getInt() method is, but I guess that you made up this class Stdin in your course some time ago.
It is not a basic java class from the JDK as far as I know.
If you are not knowing how to input something, perhaps try this code snippet:



Terminates ugly with a null pointer exception when you hit the cancel button (or with a number format exception if you enter anything else but an integer).
But does its job so far.


Yours,
Bu.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to look again at your description of a leap year.

The last two statements can't work together.

It is not divisible by 100, but it is divisible by 400.


If a number is divisible by 400, then it must be divisible by 100.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
I think you need to look again at your description of a leap year.

If a number is divisible by 400, then it must be divisible by 100.


Keith,

The description of a leap year is correct -- it is just awkwardly worded, if it is read as a single sentence.

Maybe it should read ... "A year is a leap year if it is divisible by 4, except for the cases where it is divisible by 100, then it is not a leap year, except for those exceptions where it is divisible by 400, then it is a leap year."

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn posted October 29, 2006 06:04 AM
I think you need to look again at your description of a leap year.


Anyway, Emma's code is ok.

Bu.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I understand that. I just wanted to make sure the OP went back to review the definition of leap year.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Burkhard]: Your main method also looks OK, but with one little thing that has to be done:
your local int variable y is not initialised properly.
It is not sure, that y will ever get a value, so you must define it as
int y=0; // or int y=42; or whatever.
with int y; alone, it cannot compile.


And yet, it does compile. That's because, although y is not assigned to anything when it's declared, it is definitely assigned before it's used. This is perfectly legal, though a little confusing. In this case, there is no reason to declare the variable outside the while loop in the first place. Emma could just declare it inside the loop at the point where it's first needed.

In general it's considered good practice to declare variables in the smallest possible scope that they're actually used in. That allows you to keep your code in small, easily-manageable chunks where you can more easily see all the logic involving a given variable, in close proximity. The farther statements are spread out in your code, the harder it is to see the relationships between them. Also, putting variables in small scope (when possible) minimizes the chance of accidental variable name collision (if another part of your code also uses the same variable name).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Henry]: The description of a leap year is correct -- it is just awkwardly worded, if it is read as a single sentence.

I agree with Keith - it's not merely awkward; it's contradictory. If it were code, it would throw an error. Since it's English, we can work around it. The code works OK, indicating that she knows what was meant, so no big deal, I think.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.GregorianCalendar has a method for determining if a year is leap.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

I agree with Keith - it's not merely awkward; it's contradictory. If it were code, it would throw an error. Since it's English, we can work around it. The code works OK, indicating that she knows what was meant, so no big deal, I think.



Back in college, I was thinking about learning to code in English. I just couldn't convince myself that the extra 3 years for the JD was worth it -- although English programmers do get a lot of money...

Henry
[ October 29, 2006: Message edited by: Henry Wong ]
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vlado Zajac:
java.util.GregorianCalendar has a method for determining if a year is leap.



Thanks... This is a good point to mention. Just in case, someone encounters this topic and doesn't realize that this is for a homework question.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic