aspose file tools*
The moose likes Beginning Java and the fly likes Passing two ints (hours and minutes and converting them to proper decimals) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing two ints (hours and minutes and converting them to proper decimals) ?" Watch "Passing two ints (hours and minutes and converting them to proper decimals) ?" New topic
Author

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

N Kol
Greenhorn

Joined: Sep 29, 2010
Posts: 9
How is this done?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
N Kol
Greenhorn

Joined: Sep 29, 2010
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

Joined: Feb 10, 2009
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

Joined: Sep 29, 2010
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

Joined: Feb 10, 2009
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing two ints (hours and minutes and converting them to proper decimals) ?