Agustina
Posts: 1
Joined: Wed Jun 16, 2021 11:34 am

Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 11:38 am

This is my code

Code: Select all

public class Leapyear{
  public static void main(String []args){
    for(int year =2000; year <=2020; year++){
      if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        System.out.println("Year " + year + " is a leap year");
      else
        System.out.println("Year " + year + " is not a leap year");
}
but the question is: I don't know that how to return true if it's a leap year and false if it's not a leap year.
[spamlink removed]
Last edited by Agustina on Fri Jun 18, 2021 10:16 am, edited 1 time in total.

echmain
Posts: 331
Joined: Fri Mar 04, 2016 8:26 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 11:55 am

Smells like a homework assignment and has nothing to do with Raspberry Pi.

W. H. Heydt
Posts: 14866
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 5:59 pm

Agustina wrote:
Wed Jun 16, 2021 11:38 am
This is my code

Code: Select all

public class Leapyear{
  public static void main(String []args){
    for(int year =2000; year <=2020; year++){
      if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        System.out.println("Year " + year + " is a leap year");
      else
        System.out.println("Year " + year + " is not a leap year");
}
but the question is: I don't know that how to return true if it's a leap year and false if it's not a leap year.
I see that you are restricting your range to 2000 to 2020. What do you plan to do if the request year falls before the conversion to the Gregorian calendar (varies anywhere from 1582 to 1923, depending on country)?

bls
Posts: 1600
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 6:39 pm

A bit from 1983 on the Gregorian calendar and whether 2000 should be a leap year: https://www-users.cs.york.ac.uk/susan/joke/decly.htm
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

User avatar
DougieLawson
Posts: 41470
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 7:12 pm

https://stackoverflow.com/a/11595914/3491475

Code: Select all

if ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0))
{
    /* leap year */
}
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

fanoush
Posts: 786
Joined: Mon Feb 27, 2012 2:37 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 7:31 pm

Agustina wrote:
Wed Jun 16, 2021 11:38 am
but the question is: I don't know that how to return true if it's <something> and false if it's not <something>.
Looks like the question is not about leap year at all. It is about returning a (boolean) value.
Now you just print different text messages instead of returning some value.

To return value you should move the leap year logic into separate function with return value and then in main method just use the return value to print different messages.

Heater
Posts: 18369
Joined: Tue Jul 17, 2012 3:02 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Wed Jun 16, 2021 11:25 pm

Or the question could be about returning an exit code from the program, not just returning a boolean from one function to another within the program.

To return an exit code use "System.exit(code);"

Where "code" is a "0" or "1" to indicate a leap year or not.
Memory in C++ is a leaky abstraction .

User avatar
jojopi
Posts: 3491
Joined: Tue Oct 11, 2011 8:38 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Thu Jun 17, 2021 12:28 pm

Are we actively trying to be unhelpful? (Especially Heydt.)

There should be a specification for how a particular year is requested, what range is expected, and how the result is to be returned. There is no point in us guessing what all that might be. We can all design a program, but OP will only get the marks if their program works to the specification they have.

There is no point returning (or exiting) inside a fixed for loop, so OP is missing something important in terms of how the year(s) will be passed, and needs to re-read the specification.

GlowInTheDark
Posts: 1836
Joined: Sat Nov 09, 2019 12:14 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Thu Jun 17, 2021 12:34 pm

I see that you are restricting your range to 2000 to 2020. What do you plan to do if the request year falls before the conversion to the Gregorian calendar (varies anywhere from 1582 to 1923, depending on country)?
You gotta admit: This (the above quoted text) is classic!

It's like:

Person 1: For the purposes of this discussion, we are considering only grey elephants.

Person 2: But what about green elephants???? Whacha gonna do then?

Person 1: Reading comprehension, much?
Poster of inconvenient truths.

Linux zealot and proud of it.

Heater
Posts: 18369
Joined: Tue Jul 17, 2012 3:02 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Thu Jun 17, 2021 8:13 pm

jojopi wrote:
Thu Jun 17, 2021 12:28 pm
Are we actively trying to be unhelpful? (Especially Heydt.)
No.
GlowInTheDark wrote: It's like:
...
No.

The question as posted is not clear.

Is it about returning a true of false from some function?

Or is it about returning an exit code from the entire program that indicates leap year or not?

Or why is the expected input of such a limited range of years, according to the example code given?

Or, is this homework exercise even about leap years at all? Maybe it's about one of the above, or something else.

This needs clarification from the OP before a sensible solution can be suggested.
Memory in C++ is a leaky abstraction .

User avatar
scruss
Posts: 4190
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Thu Jun 17, 2021 10:20 pm

Code: Select all

import java.time.LocalDate;

public class isleap {

    public static boolean isLeapYear(int year) {
	return (LocalDate.of(year, 3, 1).getDayOfYear() > 60);
    }

    public static void main(String[] args) {
        System.out.println("2021:");
	System.out.println(isLeapYear(2021));
        System.out.println("2020:");
	System.out.println(isLeapYear(2020));
    }

}
gives:

Code: Select all

2021:
false
2020:
true
I'm assuming that LocalDate isn't sufficiently smart to return Hijri dates or similar based on locale setting. That would be cool, but irritating for learners.

I've never written any Java. I hope never to do so again. But never, ever hand-roll date functions, because you don't know the edge cases.

Since Java's put me in a late-90s frame of mind, I'd just like to say that the time is @972 .beats.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

GlowInTheDark
Posts: 1836
Joined: Sat Nov 09, 2019 12:14 pm

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Fri Jun 18, 2021 6:29 am

I've never written any Java. I hope never to do so again.
If you've never done something, how can you be worried about doing it again?
Poster of inconvenient truths.

Linux zealot and proud of it.


User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 14058
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Create a program in Java wherein if a year is a leap year will return true otherwise false

Sun Jun 20, 2021 12:14 pm

you are missing this was a post from a post edit spambot.

Spamlink deleted and spammer banned.
kept thread intact though..

Return to “Java”