d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Don't Understand This Code. Help Plz!
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Oct 9 2018 07:59pm
Code
package com.company;

public class Main {

public static void main(String[] args) {

}




public static boolean isLeapYear(int year){
if(year < 1 || year > 9999){
return false;
}
else if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
return true;
}
return false;
}

public static int getDaysInMonth(int month, int year){
if(month < 1 || month > 12 || year < 1 || year > 9999)
return -1;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
if(isLeapYear(year)) {
return 29;
}
return 28;
default:
return -1;
}

}
}







Specifically this code here:

Code

if(isLeapYear(year))




^I don't understand what's going on in this condition/if statement.....
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Oct 9 2018 09:00pm
Code
if(isLeapYear(year))







Where does it go in first method... what condition is it testing?
Member
Posts: 11,273
Joined: Apr 26 2008
Gold: 3,303.50
Oct 10 2018 02:23am
Quote (ferf @ Oct 10 2018 03:00am)
Code
if(isLeapYear(year))







Where does it go in first method... what condition is it testing?



you have the function above :) February has different days in the month depending on the type of the year.

From wikipedia

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

The function is just testing those to check weather the year in question is a common or a leap year! Feel free to pm me if you still have doubts!
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Oct 10 2018 01:37pm
Code
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)


is the same as

Code
(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)


Otherwise, what xapeu said.

This post was edited by Klexmoo on Oct 10 2018 01:41pm
Member
Posts: 2,903
Joined: Aug 25 2009
Gold: 170.00
Oct 11 2018 06:10am
So, what Klexmoo && xapeu said.

What your function does is return 29 days (in a leap year) on month 2. And 28 if it isn't a leap year.

Edit: also, this might be al bit more fast & easy: https://stackoverflow.com/questions/8940438/number-of-days-in-particular-month-of-particular-year

This post was edited by FreeUsername on Oct 11 2018 06:13am
Member
Posts: 16,066
Joined: Jan 18 2005
Gold: 13,330.00
Oct 11 2018 10:35am
Quote (FreeUsername @ Oct 11 2018 06:10am)
So, what Klexmoo && xapeu said.

What your function does is return 29 days (in a leap year) on month 2. And 28 if it isn't a leap year.

Edit: also, this might be al bit more fast & easy: https://stackoverflow.com/questions/8940438/number-of-days-in-particular-month-of-particular-year


To be a little more clear the function is not returning 29 days or 28 days. It is returning a true or false boolean. If the year value is divisible by 4 it will return true. Otherwise it will return false.

The if statement is then checking the returned value of the boolean.
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Oct 11 2018 10:39am
Quote (Thrasher66099 @ Oct 11 2018 12:35pm)
To be a little more clear the function is not returning 29 days or 28 days. It is returning a true or false boolean. If the year value is divisible by 4 it will return true. Otherwise it will return false.

The if statement is then checking the returned value of the boolean.


Yep this..... ty
Member
Posts: 36,244
Joined: Nov 29 2005
Gold: Locked
Trader: Scammer
Jan 8 2019 12:08am
Quote
if(isLeapYear(year))

you will see countless code like that, in the future. It's JAVA
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll