3

I am writing a code that must meet a precondition, and if the conditions are all met then it will return true. I have tried multiple "if" statements but that doesn't seem to work. Nested if statements don't seem to be the answer here and I don't think "else if" statements would work. What I'm asking is, what is the correct way to do this? am I writing the if statements wrong?

heres my code:

public static boolean isLegitimate(int mon, int day, int year){

    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.


    // TODO 1: Check if a date is valid.

    //checks to see if the months are between 1 and 12
    if((mon >= 1) && (mon <= 12)) {

    }
    //checks to see if the years are greater than 1
    if (year > 0){

    }
    //checks to see if the days are between 1 and 31
    if ((day >=0) && (day <=31)){

    }

    //This checks that if the month is February, is divisible by 4 evenly,
    //and is divisible by 100 evenly, then the days can not exceed 29
    if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
        if (day >29){
            return false;
        }
    }

    return true;
}
spotTop
  • 87
  • 2
  • 7

6 Answers6

5

Just return false when a check fails. If one of the preconditions fail there is no need to check any further.

public static boolean isLegitimate(int mon, int day, int year){

    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.

    // TODO 1: Check if a date is valid.

    //checks to see if the months are between 1 and 12
    if(mon<1) return false;
    if(mon>12) return false;

    //checks to see if the years are greater than 1
    if(year<=0) return false;

    //checks to see if the days are between 1 and 31
    if(day<=0) return false;
    if(day>31) return false;

    //This checks that if the month is February, is divisible by 4 evenly,
    //and is divisible by 100 evenly, then the days can not exceed 29
    if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
        if (day >29){
            return false;
        }
    }
    return true;
}
Espen Brekke
  • 404
  • 3
  • 7
  • I guess I am bug-compatible with the original code. :-) I fix. – Espen Brekke Feb 24 '17 at 18:37
  • oops, when i read his code i just saw 'day>0'. Must've skimmed too quick. Ignore my previous comment. Though I'm not sure the change works either. I think you want `(day<1)' ;-) – mcalex Feb 24 '17 at 20:09
1

Add a boolean variable to the top of your code:

bool legit = true;

In each if statement, if the condition is false, change the value of legit. Don't change it if the value is true.

At the end of the conditions, return the variable:

return legit;

If any of the checks aren't legit, the method wil return false.

Edit: Espen's solution is more efficient (if slightly less accurate - see comment), though I'd OR out the dual clauses:

 if((mon < 1) || (mon>12)) return false;

and

if((day < 1) || (day > 31)) return false; 

Note however that this can still return invalid dates as valid, eg: 31 June

mcalex
  • 6,628
  • 5
  • 50
  • 80
1

Just another suggestion.

It may not look good for this specific case, but can help someone with a similar problem ("ask multiple conditions to return true").

public static boolean isLegitimate(int mon, int day, int year){
    return Stream.of(
            checkMonth(mon),
            checkYear(year),
            checkDay(day),
            checkFebruary(mon, day, year))
    .allMatch(check -> check);
}
Matruskan
  • 325
  • 3
  • 11
0

You can use the JodaTime API

 public static boolean isLegitmate(int mon, int day, int year){
    try {
        new DateTime().withMonthOfYear(mon).withYear(year).withDayOfMonth(day);
        return true;
    } catch (Exception e){
        return false;
    }
}
Erick Maia
  • 196
  • 6
0

Try This :

public static boolean isLegitimate(int mon, int day, int year){

if(  (mon >= 1 && mon <= 12) && (year > 0) && (day >=0 && day <=31)){
   if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0))
    if (day >29)
        return false;
    return true;
}
}
Ayman H
  • 27
  • 5
0

This breaks out your logic into multiple methods. I modified the leap year logic a bit.

public class MultipleConditions {

    public static boolean isLegitimate(int mon, int day, int year) {


        return validMonth(mon) && validYear(year) && validDay(day) && validFebDay(mon, day, year);
    }

    private static boolean validFebDay(int mon, int day, int year) {
        // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        if (mon!=2)
            return true; // Not in feb
        if (year%4 != 0)
            return day <= 28; // Not a leap year

        if (year%100 == 0) {
            return day <= 29; // Divisible by 4, but not a centurial year
        }
        // Divisible by 4, centurial year, and divisible by 400
        if (year%400==0) {
            return day <=29;
        }
        // Divisible by 4, centurial year not divisible by 400
        return day <= 28;
    }

    private static boolean validDay(int day) {
        // checks to see if the days are between 1 and 31
        return day >= 0 && day <= 31;
    }

    private static boolean validYear(int year) {
        return year > 0;
    }

    private static boolean validMonth(int mon) {
        // checks to see if the months are between 1 and 12
        return (mon >= 1) && (mon <= 12);
    }
    public static void main(String args []) {
        System.out.println(isLegitimate(2, 23, 2017));
        System.out.println(isLegitimate(2,29,2017));
        System.out.println(isLegitimate(3,31,2017));
    }
}
David Choweller
  • 1,060
  • 1
  • 8
  • 7