Results 1 to 10 of 10
  1. #1
    Pkaay is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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.

    Your code formatting is atrocious. Recommended reading:
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  4. #4
    Pkaay is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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.

    Your code formatting is atrocious. Recommended reading:
    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. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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. #6
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    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. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Rectal Exambot View Post
    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. #8
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Woah, mind = blown.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Rectal Exambot View Post
    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. #10
    Pkaay is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    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.");
    }
        }
    }

Similar Threads

  1. Happy New Year
    By sanjeevtarar in forum Entertainment
    Replies: 7
    Last Post: 12-14-2011, 06:12 AM
  2. Concatenate year from DATETIME
    By ashin in forum SWT / JFace
    Replies: 11
    Last Post: 07-30-2010, 06:34 PM
  3. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 09:50 PM
  4. Date of first day, given the week in the year and the year...
    By Lee.J.Baxter in forum Advanced Java
    Replies: 1
    Last Post: 08-26-2009, 09:48 AM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:46 AM

Posting Permissions

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