• Post Reply Bookmark Topic Watch Topic
  • New Topic

Days in the year  RSS feed

 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The year number is given (positive integer). The task is to determine the amount of
days in this year, taking into account that there are 365 days in average year  and 366 days in
leap year . A leap year is a year, which is devided by 4.
Exception is those years that are divided by 100 and are not divided by 400
(For example, the years 300, 1300, and 1900 are not leap years, but 1200 and 2000
are).

Would you  please look through my code and say what is  wrong  with it.

Thank you in  advance
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are starting off with "is divisible by 4" which means the other tests for 100 and 400 will be skipped. Start with 400 then 100 then 4.
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I  wrote
and unfortunately  it doesn't  work properly.
For example, I  enter the year 1200  or 2000 and  compiler shows that it is  not a  leap year, but  it  should  be.
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is  working, but I did it as  a  shot in the  dark. Can  somebody explain me why after changing here "year%100!=0" from == to != everything works  correctly? Thank you in  advance.



 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better for you to understand what you wrote compared to the rules your program enforces:

A year is leap if it is evenly divisible by 4 but not by 100, except if it is also evenly divisible by 400 then it is leap. Otherwise, it isn't leap.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you still can't figure it out yourself, this page gives a detailed explanation: https://sites.google.com/a/jcu.edu/cs128fall2012/daily-lessons/10-26/boolean-logic-example-leap-year
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you should put some spaces in that expression. Itsverydifficulttoreadsomethinglikethat. See what I mean?
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank  you  very  much  for  that link.

And  for  the future I will put some  spaces in  my  text.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vital Jays wrote:And  for  the future I will put some  spaces in  my  text.

I wasn't referring to your text, I was referring to your code. And going overboard with the spaces is just as bad as not having any. I was simply trying to make a point. Your response with too many spaces borders on sarcasm (or could be misconstrued as such and thus would not be very well appreciated).

Your code would have been more readable if you had written:

To make it even more readable, you could have extracted the formula to a method:
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words just  to  do the code  formatting with the button compination  Ctrl+Shift+f.

Ok, in any case thank you.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. If you refactored the way I showed earlier, you could also just write this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!