Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Passing two ints (hours and minutes and converting them to proper decimals) ?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this done?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start from scratch.....use a pencil/pen and some paper and write out the steps involved in converting hours and minutes into decimals if you were doing it without a computer. From there it should be pretty simple to convert the math into a simple program. Once you have some code written, if it isn't working then feel free to post it here and we will help.
 
N Kol
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what I got so far:

public void setLength(double length)
{
this.length= length;
int hours =(int) minutes/60;
int minutes = (int)}

so my formula would be? length = (int)(hours *60 + minutes / 60))

correct?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you looking for something like you pass in 3 hours 30 minutes and it gives you 3.5 hours? If so you would need to take another look at your formula.

Also check your data types. An int can't hold a decimal.
 
N Kol
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, then I am going to turn around and us a getter that will return a string describing the length in hours and minutes format.

if I typecast it will work
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you typecast to an int it will compile and run, but you won't get the results you are expecting/want, if I understand your requirements properly. See my code below:



This compiles and runs fine, but the first line prints 3.45 and 3.5, but the second line after the typecast prints 3 and 3.
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nicholas Dorr wrote:yes, then I am going to turn around and us a getter that will return a string describing the length in hours and minutes format.



Please be careful and precise in what you post. I have no idea what a 'string' is. Java DOES have something called a String - it that what you meant?

This may seem trivial to you, but it makes a difference. Programming is all about being very precise in what you want done. When I read a post that has mistakes like that, it makes me question the validity of everything else the poster has written, and make me think it's probably not worth my time trying to figure out what they REALLY mean.
 
Sheriff
Posts: 21450
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Nicholas Dorr wrote:yes, then I am going to turn around and us a getter that will return a string describing the length in hours and minutes format.



Please be careful and precise in what you post. I have no idea what a 'string' is. Java DOES have something called a String - it that what you meant?


I think you're a bit nitpicking here. The concept of "string" is known all throughout the programming world. java.lang.String (or shorter String) is just Java's implementation of the concept, like char* or const char* is C's implementation, std:string is C++'s implementation and Delphi's has a "primitive" string type.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!