Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Days in the year

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 9273
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Sheriff
Posts: 16943
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16943
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16943
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16943
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16943
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. If you refactored the way I showed earlier, you could also just write this:
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic