Design question -- dynamic unit conversion

Discussion in 'Java' started by Ryan Stewart, Jan 8, 2005.

  1. Ryan Stewart

    Ryan Stewart Guest

    I'm trying to come up with a good method of unit conversion for use (currently)
    in a cookbook application. Of course it would be preferable to make it as
    portable as possible. For now, I'm looking to be able to convert between english
    units of volume (tablespoons, cups, etc). A typical scenario would be something
    like:
    1) user inputs recipe containing 4 tablespoons of butter
    2) user requests a tripled recipe, which would be 12 tablespoons of butter

    That 12 tablespoons might be better represented as 3/4 cup.

    I already have a Fraction class to handle the math. My question to you is how
    would you implement the actual conversion mechanism? One way that springs to
    mind would be to have a UnitOfVolume interface which is implemented by classes
    such as Cup, TableSpoon, etc. The interface would specify methods like:
    public TeaSpoon toTeaSpoons();
    public TableSpoon toTableSpoons();
    public Cup toCups();
    public Quart toQuarts();

    That seems klunky though, since every class would have every method, including
    the one to convert to itself. Another alternative might be:
    public UnitOfMeasure convertTo(String unit);

    or
    public UnitOfMeasure convertTo(Class unitClass);

    But in either case, I think you'd have to store the relationships between every
    unit somewhere.

    How would you do it?
    Ryan Stewart, Jan 8, 2005
    #1
    1. Advertising

  2. Ryan Stewart wrote:

    > I'm trying to come up with a good method of unit conversion for use (currently)
    > in a cookbook application. Of course it would be preferable to make it as
    > portable as possible. For now, I'm looking to be able to convert between english
    > units of volume (tablespoons, cups, etc). A typical scenario would be something
    > like:
    > 1) user inputs recipe containing 4 tablespoons of butter
    > 2) user requests a tripled recipe, which would be 12 tablespoons of butter


    These sound more typical of American, rather than English,
    units. Be careful. For example, pints are different.

    >
    > That 12 tablespoons might be better represented as 3/4 cup.
    >
    > I already have a Fraction class to handle the math. My question to you is how
    > would you implement the actual conversion mechanism? One way that springs to
    > mind would be to have a UnitOfVolume interface which is implemented by classes
    > such as Cup, TableSpoon, etc. The interface would specify methods like:
    > public TeaSpoon toTeaSpoons();
    > public TableSpoon toTableSpoons();
    > public Cup toCups();
    > public Quart toQuarts();
    >
    > That seems klunky though, since every class would have every method, including
    > the one to convert to itself. Another alternative might be:
    > public UnitOfMeasure convertTo(String unit);
    >
    > or
    > public UnitOfMeasure convertTo(Class unitClass);
    >
    > But in either case, I think you'd have to store the relationships between every
    > unit somewhere.
    >
    > How would you do it?
    >
    >


    Pick one base unit. If you extended this to handle weights
    as well as volumes you would have separate base units for
    each. You can do all combinations of conversions as long as
    you know the size of each unit in terms of a common base.
    That way you enter and store N facts instead of N*N facts.

    I would have a class Unit, and an instance for each
    individual unit. Class Unit would contain the conversion code.

    Patricia
    Patricia Shanahan, Jan 8, 2005
    #2
    1. Advertising

  3. Ryan Stewart wrote:
    > I'm trying to come up with a good method of unit conversion for use (currently)
    > in a cookbook application. Of course it would be preferable to make it as
    > portable as possible. For now, I'm looking to be able to convert between english
    > units of volume (tablespoons, cups, etc). A typical scenario would be something
    > like:
    > 1) user inputs recipe containing 4 tablespoons of butter
    > 2) user requests a tripled recipe, which would be 12 tablespoons of butter
    >
    > That 12 tablespoons might be better represented as 3/4 cup.
    >
    > I already have a Fraction class to handle the math. My question to you is how
    > would you implement the actual conversion mechanism? One way that springs to
    > mind would be to have a UnitOfVolume interface which is implemented by classes
    > such as Cup, TableSpoon, etc. The interface would specify methods like:
    > public TeaSpoon toTeaSpoons();
    > public TableSpoon toTableSpoons();
    > public Cup toCups();
    > public Quart toQuarts();
    >
    > That seems klunky though, since every class would have every method, including
    > the one to convert to itself. Another alternative might be:
    > public UnitOfMeasure convertTo(String unit);
    >
    > or
    > public UnitOfMeasure convertTo(Class unitClass);
    >
    > But in either case, I think you'd have to store the relationships between every
    > unit somewhere.
    >
    > How would you do it?
    >
    >


    Search for prototype implementations of JSR-108 and see how they are
    approaching the issues of units.

    http://sourceforge.net/projects/jsr-108/

    Mark Thornton
    Mark Thornton, Jan 8, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VvanN
    Replies:
    5
    Views:
    484
    Phlip
    Apr 28, 2006
  2. Bill David
    Replies:
    2
    Views:
    270
    Arne Vajhøj
    Jun 18, 2008
  3. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    325
    Roy Smith
    Nov 18, 2011
  4. Bill Mosteller
    Replies:
    0
    Views:
    216
    Bill Mosteller
    Oct 22, 2009
  5. timr
    Replies:
    2
    Views:
    160
Loading...

Share This Page