Home > Software engineering >  Do these produce the same output?
Do these produce the same output?

Time:04-14

I'm confused by an exercise on the mooc.fi Java course. My solution apparently produces the same output as the provided solution, but my solution requires more code. I don't understand how their solution works. Is there a mistake in their solution? How does the logic in their if statement work?

My solution:

import java.util.Scanner;

public class LeapYear {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Give a year: ");
        int year = Integer.valueOf(scan.nextLine());
        boolean bool = year % 100 == 0 && year % 4 == 0;
        if (bool == true) {
            if (year % 400 == 0) {
                System.out.println("This year is a leap year.");
            } else {
                System.out.println("This year is not a leap year.");
            }

        } else if (bool == false && year % 4 == 0) {
            System.out.println("This year is a leap year.");
        } else {
            System.out.println("This year is not a leap year.");
        }

    }
}

and their solution:

import java.util.Scanner;
 
public class LeapYear {
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
 
        System.out.print("Give a year: ");
        int year = Integer.valueOf(scan.nextLine());
        if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
            System.out.println("The year is a leap year.");
        } else {
            System.out.println("The year is not a leap year.");
        }
    }
}

CodePudding user response:

Your program apparently arrives at the same output - but its logic is a bit awkward.

Take a look a this, for example:

boolean bool = year % 100 == 0 && year % 4 == 0;

So it's supposed to be true when the year is divisible by 100 as well as by 4. But mathematically, any year that is divisible by 100 is also divisible by 4. That's because

year = N * 100

is the same as

year = N * 25 * 4

So what your "bool" actually represents is "isDivisibleBy100".

So your code basically says:

if the year is divisible by 100
    if it is divisible by 400
       it is leap
    otherwise
       it is not leap
otherwise
    if it is divisible by 4
       it is leap
    otherwise
       it is not leap

Which is quite a verbose way to say the actual rule of leap years, which is - a year is leap if it is divisible by 4 but not by 100, except when divisible by 400, where it is leap.

This means, any year that is not divisible by 4 is never ever leap, no exception.

Of the years that are divisible by 4, only ones that are not divisible by 100 are leap - except if they are divisible by 400.

Which iis the same as saying:

"Of the years that are divisible by 4, only ones that are either not divisible by 100 or are divisible by 400 are leap. All the others are not leap"

Which is exactly what their code says:

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

CodePudding user response:

How does the logic in their if statement work?

The condition used in the tutorial code

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

can be expressed in English as:

the year is divisible by 4, and it is either not divisible by 100 or it is divisible by 400

  • Related