Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
N Kol
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this done?
 
W. Joe Smith
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic