# Design question -- dynamic unit conversion

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

1. ### Ryan StewartGuest

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

2. ### Patricia ShanahanGuest

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

3. ### Mark ThorntonGuest

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