Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Finding number of days in a given month

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to write a program which checks the year and month from user input. It should check wether the year is a leap-year or not. Further it should give out how many days the month has. This is my prgoram:



The year works perfectly. But how can I print out the days of the month?
 
Sheriff
Posts: 17153
298
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

Hams Petr wrote:
But how can I print out the days of the month?


You know how to print out whether a year is a leap year - it involves a call to the isLeapYear(). So wouldn't it be kind of the same way you'd print out the number of days in a month (it would involve a call to getNumberOfDays())?

By the way, the getNumberOfDays() will say that February always has 29 days, even for non leap years. (That is, you have a bug in that code)

You also have duplication of logic (line 37 and the isLeapYear() method are duplicates). And this is redundant:

Just write this:

(I'm going to double check your expression with some tests to see if it's actually valid)

Edit: I double checked with tests and your expression passes. However, it can be simplified as this:

This will evaluate the fewest boolean subexpressions as possible to determine whether or not a year is a leap year.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic