File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Leap year program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Leap year program" Watch "Leap year program" New topic
Author

Leap year program

Emma Elliott
Greenhorn

Joined: Oct 28, 2006
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, so let's see what you've got so far.


[Jess in Action][AskingGoodQuestions]
Emma Elliott
Greenhorn

Joined: Oct 28, 2006
Posts: 3
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 30, 2000
Posts: 18671
[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).


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Aug 03, 2004
Posts: 245
java.util.GregorianCalendar has a method for determining if a year is leap.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

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
 
subject: Leap year program