-1

a program which has one method that takes int "Year" as a parameter which then based on the parameter figures out whether it is a leap year or not. Theyve given a table showing which is not and which is a leap year

2010 /4 = no .. /100 = no .. /400 = no .. leap = no

2012 /4 = yes .. /100 = no .. /400 = no .. leap = yes

1900 /4 = yes .. /100 = yes .. /400 = no .. leap = no

2000 /4 = yes .. /100 = yes .. /400 = yes .. leap = yes

They also want the method to identify if a year is before the gregorian calendar 1565

Below is the current code I have done. It works for some years and doesnt for others. Obviously im doing something wrong. Any help would be greatly appreaciated if someone could inform me what Im doing right and what Im doing wrong or how best to go about this?

public class theleapyear{

  public static void main( String [] args){
    leapyear(2010);
    leapyear(2012);
    leapyear(1900);
    leapyear(2000);
    leapyear(1565);
  }

  public static void leapyear( int year){

    if (year < 1565)
      System.out.println( year + ":" + " predates the Gregorian Calendar ");

    else
    if (year % 4 != 0)
      if (year % 100 != 0)
      if (year % 400 != 0)
    {
      System.out.println( year + ":" + " is not a leap year ");
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 != 0)
        if (year % 400 != 0)
        System.out.println( year + ":" + " is a leap year ");  
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 == 0)
        if (year % 400 != 0)
        System.out.println( year + ":" + " is not a leap year ");
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 == 0)
        if (year % 400 == 0)
        System.out.println( year + ":" + " is a leap year ");

    }
  }
}
Zombo
  • 1
  • 62
  • 391
  • 407
Vince
  • 1
  • 2
  • 4
    Your code is more complicated than needed: if the year is not divisible by 4 it is definitely not a leap year, so you don't need to also test for divisibility by 100 or 400 in that case. If it *is* divisible by 4 then it is a leap year unless it is divisible by 100 but not 400. Why the "javascript" tag? Javascript != Java. – nnnnnn Sep 16 '15 at 00:22
  • 2
    possible duplicate of [Java Code for calculating Leap Year](http://stackoverflow.com/questions/1021324/java-code-for-calculating-leap-year) – Steve Sep 16 '15 at 00:24
  • What is with all the else statements? That is not how else if works.... Please read about else if https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html – epascarello Sep 16 '15 at 00:25

1 Answers1

-1

You repeat a lot of code; try resuming it using comparators.

public class theleapyear{

 public static void main( String [] args){
  leapyear(2010);
  leapyear(2012);
  leapyear(1900);
  leapyear(2000);
  leapyear(1565);
}

public static void leapyear( int year){

if (year < 1565)
  System.out.println( year + ":" + " predates the Gregorian Calendar ");

else
{
if ((year%4 != 0){
      System.out.println(year + ":" + " is not a leap year ");

 }

else {
if((year%100==0 && year%400==0)||(year%100!=0 && year%400!=0){
    System.out.println(year + ":" + " it is a leap year ");
}

else {
   System.out.println(year + ":" + " is not a leap year ");    
 }
A.Nuñez
  • 21
  • 1
  • 6
  • Thanks everyone for the help, defintely need to read up some more on this stuff but the help has been appreciated. Cheers – Vince Sep 17 '15 at 23:42