1. Member
Join Date
Oct 2010
Posts
3
Rep Power
0

## Leap Year Calculator

Hey guys,

I'm trying to create a Leap Year calculator, using only basic input and output to console, I have created enough script to generate true output to say if it is a Leap Year or not, how ever, it is a two part question, it also has to display how long until the next leapyear (number of years). And for the life of me I can't think of how to do this. My instructors are shocking and can't help me with this.

So my question is, what would I use to display how many years until the next leap year, and what tool would I use? Would I use a for loop for counting?

Any help would be appreciated :)

Java Code:
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package activity313;

/**
*
* @author 90035029
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int theYear = 412;

if (theYear % 4 == 0) {
if (theYear % 100 == 0) {

if (theYear % 400 == 0) {
System.out.println(theYear + " is a leap year.");

}
else {
System.out.println(theYear + " is not a leap year.");
}
}
else{
System.out.println(theYear + " is a leap year.");
}
}
else {
System.out.println(theYear + " is not a leap year.");

}
}
}```

2. Firstly, your code to detect if something is a leap year should be a method that returns true or false depending on whether or not it is a leap year.

Then, you will want to have a counter that increments until it finds a year that IS a leap year, by starting at the current year.

Something like this (pseudocode):
Java Code:
```year = determine current year
for (i = year, i++)
if (isLeapYear(i))
print "This is the next leap year: " + i
break the loop```

3. My instructors are shocking and can't help me with this.
Yeah yeah it's always the instructors that are hopeless, never the students. That's why only a minuscule percentage of students have to take recourse to a forum to get their homework done.

If the instructors were really as bad as some of you would have us believe, we'd be swamped with homework questions. Change your attitude.

Code Conventions for the Java(TM) Programming Language: Contents

db

4. Member
Join Date
Oct 2010
Posts
3
Rep Power
0
Originally Posted by Darryl.Burke
Yeah yeah it's always the instructors that are hopeless, never the students. That's why only a minuscule percentage of students have to take recourse to a forum to get their homework done.

If the instructors were really as bad as some of you would have us believe, we'd be swamped with homework questions. Change your attitude.

Code Conventions for the Java(TM) Programming Language: Contents

db
I'm sorry but I don't think you're right, to be qualified to teach programming, I think you should be able to answer a question as simple as mine. As for my structure, I know it is. I have only been doing this for a week, reading from a written textbook from my course, which has horrible english and punctuation. I also think they should learn to speak english before they teach in an english spoken country.

5. Originally Posted by Darryl.Burke
Yeah yeah it's always the instructors that are hopeless, never the students. That's why only a minuscule percentage of students have to take recourse to a forum to get their homework done.

If the instructors were really as bad as some of you would have us believe, we'd be swamped with homework questions. Change your attitude.
db
I disagree. When I took a junior programming course (despite knowing more than the teacher), if I hadn't known anything about programming I **know** I wouldn't have done very well at all. The guy just couldn't teach.

6. Member
Join Date
Aug 2010
Posts
16
Rep Power
0
year % 4 is the amount remainder right? So its the amount of years that have passed since the last leap year.

Since there is 4 years between each leap year, shouldn't it be.....

yearsTilLeap = 4 - lastLeapYear?
Last edited by Rectal Exambot; 10-12-2010 at 10:05 AM.

7. Originally Posted by Rectal Exambot
year % 4 is the amount remainder right? So its the amount of years that have passed since the last leap year.

Since there is 4 years between each leap year, shouldn't it be.....

yearsTilLeap = 4 - lastLeapYear?
Nope, e.g. 1897+3 == 1900 which isn't a leap year.

kind regards,

Jos

8. Member
Join Date
Aug 2010
Posts
16
Rep Power
0
Woah, mind = blown.

9. Originally Posted by Rectal Exambot
Woah, mind = blown.
Mwah, it isn't that bad; your method gives a 'potential' leap year; there already is a isLeapYear( ... ) method (if all is well). If the 'potential' leap year isn't a leap year add another four years to make certain that the resulting year is a leap year.

kind regards,

Jos

10. Member
Join Date
Oct 2010
Posts
3
Rep Power
0
Hey guys have finally sorted it out this morning, came to me in my sleep strangely enough, I woke up and wrote it down before I forgot!

Thanks for everyones inputs and ideas. Onto the next activity!

Java Code:
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package activity313;

/**
*
* @author 90035029
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int theYear = 2;
boolean leapYear = false;
int count = 0;

if (theYear % 4 == 0) {

leapYear = true;

if (theYear % 100 == 0) {

if  (theYear % 400 == 0){

System.out.println(theYear + " is a leap year.");
leapYear = true;

}
else{

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

}

}
else{

System.out.println(theYear + " is a leap year.");
leapYear = true;
}

}

if (leapYear == false) {
count = 4 - theYear % 4;
System.out.println("Year: " + theYear + " has " + count + ", year(s) until the next Leap Year.");
}
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•