Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java leap year code problems

import java.util.Scanner;

public class Hw2JamesVaughn  {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);        
        System.out.print("Enter a year: ");
        int year = input.nextInt();
        if((year < 1582) == (year % 4==0))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");

        if((year > 1582) == (year % 100 != 0) || (year % 400 == 0))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");

    }                
}

This is the assignment.

(To determine if a particular year is a leap year, use the following logic:

  • the year must be divisible by 4
  • starting from 1582, if the year is divisible by 100, it must also be divisible by 400 Thus, the year 1700 is not a leap year, but 2000 is. However, 1500 is leap year since it was before 1582, the adoption year of Gregorian calendar. Your program will ask for a year, and then display whether the year is leap year or not.)

I have gotten this far with my java leap year program but its not working! Ive been working on this and i have no idea what is wrong.

like image 249
Rick Vaughn Avatar asked Sep 01 '14 21:09

Rick Vaughn


People also ask

How do you solve a leap year problem?

Check if the number is evenly divisible by 400 to confirm a leap year. If a year is divisible by 100, but not 400, then it is not a leap year. If a year is divisible by both 100 and 400, then it is a leap year. For example, 1900 is evenly divisible by 100, but not 400 since it gives you a result of 4.75.

Why 2000 was not a leap year?

The rule is that if the year is divisible by 100 and not divisible by 400, leap year is skipped. The year 2000 was a leap year, for example, but the years 1700, 1800, and 1900 were not. The next time a leap year will be skipped is the year 2100.


1 Answers

Firstly, this if((year < 1582) == (year % 4==0)) checks boolean equality. I think you wanted an if((year < 1582) && (year % 4==0)) but I'm afraid that still doesn't fix your logic.

I suggest you start by creating a method. The first part should test if the year is less then 1582. If so, return true if it's a multiple of 4. The second part is well described on Wikipedia here. Putting it together gives something like,

private static boolean isLeapYear(int year) {
    if (year < 1582) {
        return (year % 4 == 0);
    }
    /*
     * Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year
     */
    if (year % 4 != 0) {
        /*
         * if (year is not divisible by 4) then (it is a common year)
         */
        return false;
    } else if (year % 100 != 0) {
        /*
         * else if (year is not divisible by 100) then (it is a leap year)
         */
        return true;
    }
    /*
     * else if (year is not divisible by 400) then (it is a common year)
     * else (it is a leap year)
     */
    return (year % 400 == 0);
}

Then you can use printf to output the result,

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a year: ");
    int year = input.nextInt();
    System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a"
            : "is not a");
}

Finally, your original code could be implemented like -

if (year < 1582 && year % 4 == 0)
    System.out.println(year + " is a leap year");
else if (year < 1582)
    System.out.println(year + " is not a leap year");
else if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
    System.out.println(year + " is a leap year");
else
    System.out.println(year + " is not a leap year");
like image 115
Elliott Frisch Avatar answered Oct 14 '22 08:10

Elliott Frisch