Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Design question -- dynamic unit conversion

Reply
Thread Tools

Design question -- dynamic unit conversion

 
 
Ryan Stewart
Guest
Posts: n/a
 
      01-08-2005
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?


 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-08-2005
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



 
Reply With Quote
 
 
 
 
Mark Thornton
Guest
Posts: n/a
 
      01-08-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Friendly Easy To Install Unit For Voip Like Vonage Unit SuggestionsPlease? Heidy UK VOIP 1 06-22-2007 09:53 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
connect a source unit with an S-Video connection to a receiving unit that has an RCA composite video connection ? worth it ? OCZ Guy DVD Video 6 08-01-2004 05:44 PM
Connect a source unit DVD with an S-Video connection to a receiving unit TV that has an RCA composite video connection Worth it. OCZ Guy Computer Information 0 07-31-2004 12:29 PM



Advertisments