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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Art and Science of Java - Programming exercise chapter 3

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:

Howdy all

I wonder if anyone would be able to help me. I am working through a programming task where I need to convert feet and inches to centimetres. The program seems to run fine [please let me know if I have made a mistake] but the output I am supposed to emulate includes something I am just not able to get right (see pic).

I am not sure how to get the result 180.0cm , I can get 180cm or 180.34cm but I don't know how I can get 180 to .0cm. I thought maybe it something to do with asking for one decimal place? How would you do this?, but then would this round to 180.3 as 1dp. Thanks

FeetAndInchesToCM.png

Ranch Hand
Posts: 40
• Number of slices to send:
Optional 'thank-you' note:
hi
check DecimalFormat class
import this class and try this

DecimalFormat myFormatter = new DecimalFormat("###.0");

You should get what you want.

Thank you.

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
Personally, I find your constant names confusing. Both are for converting english to metric, but you don't maintain the same order in the names. I would either do them as

CENTIMETERS_PER_INCH
CENTIMETERS_PER_FOOT

or both as

FEET_TO_CM
INCHES_TO_CM

And really, you don't need both, as you could convert feet to inches, add them to your inches then do one conversion. You'd still need two constants (FEET_TO_INCHES, for example), but since doing an int to a float always brings in some precision loss, only doing one might make your results slightly more accurate. Although, if you only care about things to 1 decimal place, it may not matter.

Marshal
Posts: 79389
377
• Number of slices to send:
Optional 'thank-you' note:
Since the correct answer for 5'11" is 180.34, how on earth are you going to get 180.0 from it?

June Og
Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
Hi all

Thanks Mezan I have looked at the DecimalFormat class and I can get the answer output to show as 1dp now.

Fred, the naming of the constants were based on what was used in the book for a previous example, its good to know how viable they would be in the "real world".

Campbell, yep thats the bit I still dont get! So either the book has a typo or I am missing something.

Campbell Ritchie
Marshal
Posts: 79389
377
• Number of slices to send:
Optional 'thank-you' note:
That is a typo in the book.

fred rosenberger
lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:

June Ogterop wrote:its good to know how viable they would be in the "real world"

I'm not sure i qualify as 'real world', since I have not done any java programming professionally for 3 or so years. IMHO, the most important thing in writing code is making it as easy as possible to read. Remember, someday, someone else is going to look at your code. There is an adage that goes something like "Always code as if the next person to read your code is a homicidal maniac who knows your home address."

June Og
Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
Thanks Campbell.

fred rosenberger wrote:

June Ogterop wrote:its good to know how viable they would be in the "real world"

I'm not sure i qualify as 'real world', since I have not done any java programming professionally for 3 or so years. IMHO, the most important thing in writing code is making it as easy as possible to read. Remember, someday, someone else is going to look at your code. There is an adage that goes something like "Always code as if the next person to read your code is a homicidal maniac who knows your home address."

Fred, when its put in that way!.... I will be sure to remember that, thanks.

Campbell Ritchie
Marshal
Posts: 79389
377
• Number of slices to send:
Optional 'thank-you' note:
Good thing you picked up on Fred's hint. Otherwise he would have used a stronger threat, like . . . "Imagine Campbell Ritchie is going to read your code and he knows where you live."

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?