From the course: Learning Java 11
Unlock the full course today
Join today to access over 23,200 courses taught by industry experts.
Solution: Leap year checker - Java Tutorial
From the course: Learning Java 11
Solution: Leap year checker
- [Instructor] Let's write some code to determine if a given year is a leap year. We know if a year is divisible by four, it is a leap year. To check if this is true for our year we can use an IF statement. Here we're using the modulo operator to find the remainder of the year divided by four. If the remainder equals zero, then the year is evenly divisible by four and it is a leap year. Now, if that year is also divisible by a hundred, it is not a leap year unless it's also divisible by 400. Let's add those extra IF statements. Here if the year's evenly divisible by four and it's evenly divisible by 100, but not divisible by 400, we return false. If it's divisible by all three numbers, we return true. If the year is divisible by four but not 100, we also return true. Let's run our code. Our implementation works as expected. However, it is a little hard to read and understand. To collapse this logic, we can use Boolean operators. These operators will allow us to combine the conditions…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.