• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How To examine minutes parameter in Time class?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to define a class called time which will hold integers for hours and minutes.
my problem now is: how to examine the minutes parameter...if it is greater than 59, then how to transfer the multiples of 60
over to the hours portion of the Time object. (e.g., new Time(10,93) results in 11 hours and 33
minutes).

any idea how can I do this?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think this design is flawed. You should not have constructor take responsibility for such conversion. Ideally the class's contract should specify it to be clients responsibility and you should have validation logic to ensure that data in min and hrs are within allowable limits.

If at all you want then you can expose a utility method which can handle such conversion. e.g int evaluateHours(int minutes). Similarly for hours.

Hope it helps.

Thanks,
Satish
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two ways to handle this issue:
1) throw an IllegalArgumentException if the minutes value is < 0 or >= 60.
2) use / to get the number of full hours from the minutes, which you add to the given number of hours, and use % to cut get rid of these hours.

Personally, I'd go for option 1. I see it as an error to create a time object with 10 hours and 93 minutes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic