Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting units in Java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
deltatux




PostPosted: Thu May 20, 2010 10:50 pm   Post subject: Converting units in Java?

Hey guys,

So last year during my summer break, I got so bored, that I wrote a nifty Java applet/application to do some unit conversions. Unfortunately, I just realized that my feet/inches to cm conversions are grossly inaccurate.

Here's my applet that I hosted on my website here:
http://www.kwokinator.com/publications/software/converitall

When I enter 5 ft. 7 in., I selected the ft measurement in my converter and typed 5.7 ft, it gave me 173.74 cm.

In reality, it should be 170.18cm. I used this formula in my calculations:
code:

} else if (strOrigUnit.equals("ft") && strConvUnit.equals("cm")){
            return (dblOrigAmt * 30.48);


Am I missing something here? Note, I've learned exclusively to use metric units so I'm not too well versed in imperial units.

I'm also thinking my cm to ft converter is wrong as well...

Please help.

Cheers,
deltatux
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu May 20, 2010 11:24 pm   Post subject: RE:Converting units in Java?

There are 12 inches in a foot. 5.7 feet is 5 feet + 7/10th of a foot, which is 8.4 inches. You have 1.4 extra inches that you are not expecting. Guess how many cm that is Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
deltatux




PostPosted: Thu May 20, 2010 11:56 pm   Post subject: RE:Converting units in Java?

Uhh, so how should I put that in code form? Like I said, I'm not well versed with imperial units.

Sorry for sounding stupid lol.

Thanks,
deltatux
Tony




PostPosted: Fri May 21, 2010 12:11 am   Post subject: RE:Converting units in Java?

what I'm saying is that 5.7 ft not= 5 ft 7 in

It's not a problem with the code, but with the UI.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
deltatux




PostPosted: Fri May 21, 2010 4:49 pm   Post subject: Re: Converting units in Java?

Hmmm... so how does one enter in 5 ft. 7 inches in a single line or do I have to say if ft. is selected it should have two boxes. One stating feet, another stating inches?

Thanks,
deltatux
TheGuardian001




PostPosted: Fri May 21, 2010 6:13 pm   Post subject: Re: Converting units in Java?

One solution would be to have separate boxes for each input, however that isn't exactly necessary. You could also just take whatever number is given after the decimal as a single number and divide it by 12.
IE, for input (as Feet.Inches, not Feet.1/10th of Foot):
code:

1.6
2.3
0.12

You would take whatever is after the decimal:
code:

6
3
12

And divide it by 12, giving you the correct decimal representation of that many inches:
code:

6/12 = 0.5 + 1 foot = 1.5 feet
3/12 = 0.25 + 2 feet = 1.25 feet
12/12 = 1 + 0 feet = 1 foot

Even if you used a separate box, you would still need to divide the inches value by 12, in order for your converter to work correctly (since math uses tenths, not twelfths.) Both options will work, So really it's just down to whatever is easier for you to implement.
deltatux




PostPosted: Fri May 21, 2010 10:54 pm   Post subject: RE:Converting units in Java?

good point, however, I don't quite remember how to select numbers behind a decimal per se.

What function should I be using? Please refresh my memory.

Thanks,
deltatux
DemonWasp




PostPosted: Sat May 22, 2010 1:19 am   Post subject: RE:Converting units in Java?

You should be able to come up with a solution using just indexOf and substring if you think for a moment.
Sponsor
Sponsor
Sponsor
sponsor
deltatux




PostPosted: Sat May 22, 2010 2:13 am   Post subject: RE:Converting units in Java?

Alright, thanks, I'll take a look at it later today when I'm awake.

Cheers,
deltatux
deltatux




PostPosted: Fri May 28, 2010 4:18 pm   Post subject: RE:Converting units in Java?

Alright I've been thinking about it for a while and I'm not sure how you can use indexOf and substring to get this working.

Do I use the indexOf function to find all the numbers before the decimal placement? If so is it something like this?

code:

strOrigIdx = strOrigAmt.indexOf(".");
strOrigAmt.substring(strOrigIdx);


I'm really confused.

Thanks,
deltatux
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: