How to implement unit converter in java

How can I implement unit converter in Java ??? I was thinking about having an abstract base class:

public abstract class Unit { ... public void convertTo(Unit unit); } 

Then, each class like the Meter Kilometer Inch Centimeter Millimeter ... is derived from this base class Unit. All units of length will be in the com.unitconverter.distance package, then the package, com.unitconverter.energy, for energy, etc. Etc. So is this the best way to implement a unit converter? Or is there a better or easier way?

+6
java abstract-class
source share
7 answers

A lot of work has been done on this issue, including JSR 108 (discontinued) and JSR-275 (rejected). See JScience and UnitsOfMeasure for the latest implementations.

+12
source share

You should check how java.util.concurrent.TimeUnit is implemented as a reference. This is an enumeration that supports the conversion operation (..).

+4
source share

To implement the Unit converter, you go from one Unit to another. So I would have a method called convertTo() that would take one Unit object and return another Unit object.

Each subclass of the Unit class will have its own definition of how it is converted to some intermediate Unit , and how it converts the value from some intermediate Unit to its own Unit .

To convert it, your convertTo() method will call its own method to convert itself to an alternate Unit , and then call another method to convert that intermediate Unit to the type that was passed as the parameter (because the one that was passed has your own convertFrom() ) method.

+2
source share

Do not forget, although you need to model the concept of Unit, it is actually the "unit + quantity" that you are converting. In addition, the concept of dimension is important because it restricts which transformations make sense (i.e., a meter in the yard, as well as lengths, and Celius in Watts will not).

+1
source share

As a converter, the units of measurement are data and do not actually belong to the code.

You can do some really awesome things, however, if you break your units into β€œbasic” components (length, temperature, time, ...)

So you may have a database that looks like this:

minute = 1 x Time (where Time is the base unit of time)

second = time / 60

hour = time * 60

meter = 1 x distance

Clometer = 1000 x distance

...

The best part is that you begin to get real flexibility with formulas. For example, if I had some kind of information around, like the fact that the snail I was observing moved 20 cm in 3 minutes, I could easily pass this to the speed equation (speed = distance / time) to get β€œRoot” speed number, then request the results in terms of different units (for example, miles and hours) so that I instantly get the result in miles per hour.

You even know what information you don’t have - for example, if you give a distance and ask for speed, she may say "you still need time." Or, if you give distance and speed, it can calculate the time in units of your choice.

In any case, all the data. You probably shouldn't even have different classes for distance and time.

I believe that I would use a fixed set of measuring objects, as described above, which could be used to search for conversions (unit name, unit type, conversion factor), for example ("Minutes", TIME, 1)

When the user enters a value with a unit (let them say that the user enters 3 hours), I just look at the block ("Clock"), compute the conversion (180, TIME) and save (180, TIME) somewhere.

When they requested a countdown in a few minutes, I looked at the conversion in minutes ("Minutes", TIME, 1) and used a scaling factor (1) to determine that you need to print 180 minutes.

If the user set a value of 3 hours and asked for speed in meters per second, you can request a distance (which the user can specify using any type of distance) and easily convert to the desired result. Converting to base units and using scaling factors almost completely eliminates the difficulties.

Justification / Argumentation:

My assertion that units and conversion factors should not be indicated in the code is likely to be called into question ...

My reasoning is that you should NEVER have objects that do not have a single business logic.

The only difference between minutes and seconds will be a formula that will only change in constant (at any given time you can use the second x 60 or hour * (1/60), so the constant (1, 60, 1/60) cannot create a new class in itself would be absolutely criminal.

This applies to all major types of measurements; none of them have a unique business logic.

+1
source share

Using abstract classes is good when there is some default behavior that you want to implement, as well as some methods that you want to provide for performing some non-standard actions. If you have only one method that you want to ensure that your inheriting Unit objects are implemented, you must use the interface.

0
source share

You can implement unit conversion using a sparse plot. Create a border between two units if there is a transformation between them. Erotic sets in transitive closure are units of the same type (length, time, etc.).

0
source share

All Articles