How to resolve the algorithm Leap year step by step in the Java programming language
How to resolve the algorithm Leap year step by step in the Java programming language
Table of Contents
Problem Statement
Determine whether a given year is a leap year in the Gregorian calendar.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Leap year step by step in the Java programming language
The provided Java code has two classes, Leapyear
and IsLeap
, each demonstrating different approaches to determine whether a given year is a leap year. Here's a breakdown of both classes:
1. Leapyear
Class:
-
This class uses the
GregorianCalendar
class fromjava.util
to check if a year is a leap year. -
It defines an array
yrs
containing several years, including some that are leap years and some that are not. -
It then iterates through the years in the
yrs
array and prints the results of checking whether each year is a leap year using both theGregorianCalendar
methodisLeapYear(year)
and a customisLeapYear(year)
method defined within the class. -
The
isLeapYear(year)
method of theLeapyear
class implements a custom logic to determine leap years based on the following rules:
- If a year is divisible by 100, it is a leap year only if it is also divisible by 400.
- If a year is not divisible by 100, it is a leap year if it is divisible by 4.
2. IsLeap
Class:
- This class uses the
Year
class fromjava.time
(introduced in Java 8) to check if a year is a leap year. - It has a simple
main
method that prints whether the year 2004 is a leap year using theisLeap(2004)
method of theYear
class.
The output of the Leapyear
class would be:
The year 1800 is leaper: false / false.
The year 1900 is leaper: false / false.
The year 1994 is leaper: false / false.
The year 1998 is leaper: true / true.
The year 1999 is leaper: false / false.
The year 2000 is leaper: true / true.
The year 2001 is leaper: false / false.
The year 2004 is leaper: true / true.
The year 2100 is leaper: false / false.
The output of the IsLeap
class would be:
true
In summary, both the Leapyear
and IsLeap
classes demonstrate different ways to determine whether a given year is a leap year. The Leapyear
class uses a custom logic, while the IsLeap
class leverages the Year
class introduced in Java 8 for this purpose.
Source code in the java programming language
import java.util.GregorianCalendar;
import java.text.MessageFormat;
public class Leapyear{
public static void main(String[] argv){
int[] yrs = {1800,1900,1994,1998,1999,2000,2001,2004,2100};
GregorianCalendar cal = new GregorianCalendar();
for(int year : yrs){
System.err.println(MessageFormat.format("The year {0,number,#} is leaper: {1} / {2}.",
year, cal.isLeapYear(year), isLeapYear(year)));
}
}
public static boolean isLeapYear(int year){
return (year % 100 == 0) ? (year % 400 == 0) : (year % 4 == 0);
}
}
import java.time.Year;
public class IsLeap {
public static void main(String[] args) {
System.out.println(Year.isLeap(2004));
}
}
You may also check:How to resolve the algorithm Function definition step by step in the Forth programming language
You may also check:How to resolve the algorithm French Republican calendar step by step in the Go programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Java programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the EasyLang programming language