Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:08:27+00:00 2026-05-24T13:08:27+00:00

/* *Find if a year is leap or not */ public class LeapYear{ private

  • 0
/*
 *Find if a year is leap or not
 */

public class LeapYear{

    private static int leapYear;

    public void setLeapYear(int leapYear){

        this.leapYear = leapYear;

    }// end method


    public static void main (String[] args) {
        LeapYear leap = new LeapYear();
        leap.setLeapYear(2010);
        leap.setLeapYear(2008);
        leap.setLeapYear(1900);
        leap.setLeapYear(2000);
        leap.setLeapYear(1565);

        // Is it Divisible by 4?
        if (leapYear % 4 == 0) {

            // Is it Divisible by 4 but not 100?
            if (leapYear % 100 != 0) {
                System.out.println(leapYear + ": is a leap year.");
            }
            // Is it Divisible by 4 and 100 and 400?
            else if (leapYear % 400 == 0) {
                System.out.println(leapYear + ": is a leap year.");
            }
            // It is Divisible by 4 and 100 but not 400!
            else {
                System.out.println(leapYear + ": is not a leap year.");
            }
        }
        // It is not divisible by 4.
        else {
            System.out.println(leapYear + ": is not a leap year.");
        }
    }
}

I am new to Java and I wrote this code so that it would call for all five years into the boolean and generate answers for all of them. However it only calls the last one. How would I do this right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:08:27+00:00Added an answer on May 24, 2026 at 1:08 pm

    You need use separate object for each year or at least call the Leap Year checking method as soon as you crated the object for that year.

    What you have is a series of call to a function that assigns a value to an attribute of the same object. Therefore, only the last statement has the effect as previous values are overwritten.

    On additional note, your code doesn’t seem to be properly organized. Whey do you make the checkings in Main and it seems that leapYear isn’t defined anywhere.

    Perhaps, you may want to define a function that returns true/false depending on the value of the passed parameter or the value of the year stored in object.

    The code may look something like this:

    leap.setLeapYear(2010);    // leap.leapYear = 2010;
    System.out.println(leap.isLeapYear());
    
    leap.setLeapYear(2008);    // leap.leapYear = 2008;
    System.out.println(leap.isLeapYear());
    leap.setLeapYear(1900);    // leap.leapYear = 1900;
    System.out.println(leap.isLeapYear());
    leap.setLeapYear(2000);
    System.out.println(leap.isLeapYear());
    leap.setLeapYear(1565);
    System.out.println(leap.isLeapYear());
    

    You have to define isLeapYear() by moving the checks in main to that function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find this design pattern comes up a lot: try: year = int(request.GET['year']) except:
Possible Duplicate: how to find leap year? What will be the exact definition of
I have a (semantically incorrect) HTML like this: <ul> <li class=year>2009</li> <li><a href=project1.html>Project 1</a></li>
I would like to find out the date of the Monday in this year's
I could not find a method in Flex Date object to get the week
I'm trying to find the number of employees joined over a calender year, broken
Is there a way to find the weeknumber within the year using PHP?
I want to find fiscal month and year based on a date. How Can
I have the birth date from particular year, i want to find the day
Happy New Year Everyone! I have this existing php code I did when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.