• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Art and Science of Java - Programming exercise chapter 3

 
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
[Thumbnail for FeetAndInchesToCM.png]
The output I need to get as a result of running my program with input 5 then 11
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a typo in the book.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
reply
    Bookmark Topic Watch Topic
  • New Topic