Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

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

N Kol
Greenhorn
Posts: 9
How is this done?

W. Joe Smith
Ranch Hand
Posts: 710
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
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
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
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
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: 12234
36
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
Posts: 20709
68
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.