Java assignment 07: Find the leap years

Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
 01   02   03   04   05   06   07   08   09   By title 

Write a program that allows the user to enter a year. After inserting a year and pressing the enter button, the program determines whether the inserted year is a leap year.

Note: A leap year is divisible by 4 with no remainder.

Java assignment 07: Find the leap years

Level: Normal

import java.util.Scanner;

public class CheckYear
{
  public boolean isLeapYear(int year)
  {
    // write your code here
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Insert a year: ");
    int insertedYear = input.nextInt();
    CheckYear cy = new CheckYear();
    if(cy.isLeapYear(insertedYear))
    {
      System.out.println(insertedYear +
          " is a leap year");
    }
    else
    {
      System.out.println(insertedYear +
          " is not a leap year");
    }
    input.close();
  }
}
/*
Below, the user inserted the year 2020
Insert a year: 2020
2020 is a leap year
 */
Author: Sar Maroof

Answer explanation

The leap years from 2021 till 2053 are 2024 2028 2032 2036 2040 2044 2048 2052.
The following code writes the leap years mentioned above.

import java.util.Scanner;

public class CheckYear
{
  public boolean isLeapYear(int year)
  {
    if(year % 4 == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Insert a year: ");
    int insertedYear = input.nextInt();
    CheckYear cy = new CheckYear();
    if(cy.isLeapYear(insertedYear))
    {
      System.out.println(insertedYear +
          " is a leap year");
    }
    else
    {
      System.out.println(insertedYear +
          " is not a leap year");
    }
    input.close();
  }
}

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *