• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Unit conversion program

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to convert cm into feet and inches!

I've tried everything and just cannot seem to get it to work. I can do cm to feet then cm to inches seperately but can't get it to work out the feet remainder inches.

Any help with this would be very much appreciated!

Thanks, Wes

[EJFH: Supplied new subject line (was "Urgent urgent help help please!")
[ April 19, 2005: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about posting your codes out & let us see where you might have missed out something?
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
12 inches = 1 foot
1 centimeter = 0.393700787 inches

SO to convert 150 cm in feet, you should use
150/2.54 = 59.056 inches

to convert 59 inches in feet, you should use
59.056/12 = 4.92 feet

The conversion factor to convert inches to cm is 2.54.
i.e if you want to convert 10 inches in cm you should use 10*2.54=25.4

Hope this helps
 
Wes Duxbury
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i have so far:

int cm = Integer.parseInt(txtCm.getText());
double answer = cm / 2.54;
double feet = answer / 12;

txtAnswer.setText("" + feet);

Now I'm completely stumped! That just shows feet, and as a really long number, e.g, 100cm comes out as 3.2808398950131235!!!

I need it to be 3ft 3inches.

I have tried using chatAt and Math.round but i keep getting the dreaded red wiggly line!
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2.54 centimeters in an inch and 12 inches in a foot. You can try something like:



The key of course is java.lang.Math.floor.

In the future I suggest not using subject lines like "Urgent Help Please Please Please!" People are much less likely to reply because you give no indication what your problem is. Something like, "Converting between centimeters and inches and feet" might be more helpful.

Chris
 
Wes Duxbury
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is i can't seem to import java.lang.math.floor, it says javadoc not found!

Good point about the title, didn't think of that! Too busy getting frustrated with this program!
 
Chris De Vries
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just put "Math.floor(...)" in your code. java.lang classes are already imported, I just added the full package name so you could look it up easily. Math is a class containing mostly static functions.

Under j2se1.5 or jdk5 or whatever they call it now, you can do a static import. So you just need to add



and then you can use "floor()" in your code without the class name.

Chris
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now I'm completely stumped! That just shows feet, and as a really long number, e.g, 100 cm comes out as 3.2808398950131235!!!

I need it to be 3ft 3inches.



What I would suggest you to do is to obtain the feet & inches separately. First, you would need to find out the integral foot component, without rounding off. You just need to do a Math.floor() on the answer you had to get 3.0. Then by using Math.round, you'll get a long integer value 3.

Next, you'd need to find out the rounded off inches portion. By subtracting the integral foot component from the answer, you'll get 0.2808398950131235. So the remaining challenge would be to convert this to an integer value of 3 (inches component). I'm sure you're able to figure out the remaining steps.

 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The thing is i can't seem to import java.lang.math.floor, it says javadoc not found!



The package name is wrong. Anyway, as pointed out, you do not need to explicitly import any of the classes under java.lang package. You can simply just use the static methods from the Math class, e.g., Math.round().
 
reply
    Bookmark Topic Watch Topic
  • New Topic