Home > database >  Java Udemy course leap year calculator - can't figure out what am I doing wrong
Java Udemy course leap year calculator - can't figure out what am I doing wrong

Time:08-05

I am unable to find out why my code isn't working. I have figured out another way to solve this problem, but I want to understand why it won't work as I did. That's all I want to understand. I don't need it to be solved.

Leap Year Calculator Write a method isLeapYear with a parameter of type int named year.

The parameter needs to be greater than or equal to 1 and less than or equal to 9999.

If the parameter is not in that range return false.

Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is a leap year, otherwise return false.

To determine whether a year is a leap year, follow these steps:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days). The method isLeapYear needs to return true.
  5. The year is not a leap year (it has 365 days). The method isLeapYear needs to return false.

The following years are not leap years: 1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600 This is because they are evenly divisible by 100 but not by 400.

The following years are leap years: 1600, 2000, 2400 This is because they are evenly divisible by both 100 and 400.

Examples of input/output:

isLeapYear(-1600); → should return false since the parameter is not in range (1-9999)

isLeapYear(1600); → should return true since 1600 is a leap year

isLeapYear(2017); → should return false since 2017 is not a leap year

isLeapYear(2000); → should return true because 2000 is a leap year

NOTE: The method isLeapYear needs to be defined as public static ​like we have been doing so far in the course. NOTE: Do not add a main method to solution code.





My Code:
public class LeapYear {

    public static boolean isLeapYear (int year) {

        //Out of range
        if ( year >= 1 && year <= 9999) {
            return false;
        }
        //calculating if the year is a leap year or not
        if ( year % 4 == 0 ) {
            if ( year % 100 == 0) {
                if ( year % 400 == 0) {
                    return true;
                }else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

CodePudding user response:

I copied pasted your instructions/your code:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.

  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.

  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.

  4. The year is a leap year (it has 366 days). The method isLeapYear needs to return true.

  5. The year is not a leap year (it has 365 days). The method isLeapYear needs to return false.

     public class LeapYear {
    
       public static boolean isLeapYear (int year) {
         //0) greater than or equal to 1 and less than or equal to 9999.
         if (year < 1 || year > 9999) {
           return false;
         }
    
         //1) If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5
         if (year % 4 != 0 ) {
           return false;
         }
    
         //2) If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
         if (year % 100 != 0) {
           return true;
         }
    
         //3) If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5
         if (year % 400 != 0) {
           return false;
         }
    
         //4) The year is a leap year (it has 366 days)
         return true;
       }
     }
    

Often, a "trick" to simplify your expressions is to realize things like:

  • "x is greater than or equal to 1" is equivalent to "x is not less than 1"
  • "y is evenly divisible by 4" is equivalent to "not y % 4 != 0"
  • Etc.

'Hope that helps...

CodePudding user response:

If you rearrange your steps slightly, you can do it in fewer steps as follows.

public class LeapYear {

   public static boolean isLeapYear (int year) {
       // check range - only years between 1 and 9999 inclusive allowed
      if ( year < 1 || year > 9999) {
            return false;
       }

      // If the year is evenly divisible by 400 return true.
      if (year % 400 == 0) {
          return true;
      }

      // If the year is evenly divisible 100 return false
      if (year % 100 == 0) {
         return false;
      }

      // if year is divisible by 4 return true/ else false.
      
      return year % 4 == 0; //returns true or false depending on year
   }
}

CodePudding user response:

First thing I can notice is:

//Out of range
if ( year >= 1 && year <= 9999) {
   return false;
}

Sould be:

//Out of range
if ( year < 1 || year > 9999) {
   return false;
}

Because it's written the wrong way round.

Aside from that the code seems fine, although it could be improved.

Can you also give an example of how the output you are getting differs from the output you want?

  •  Tags:  
  • java
  • Related