Class UnitConverter

java.lang.Object
  |
  +--java.util.Observable
        |
        +--UnitConverter

public class UnitConverter
extends java.util.Observable

This class implements unit conversions between Metric and IP. The root unit system for all calculations is always metric. Input and output values can be metric or I-P (inch-pound). The class contains a state variable, set using the static setUnitSystem() method, which sets the unit system that will be used for all conversions.

Methods that do input or output should create an instance of this class for each type of unit with which they deal. They should then run input data through getMetricValue() to get the value that will be used for all calculations. Output data should be run through the getConvertedValue() method to get the value that will be displayed. If this is done, unit coversion is otherwise transparent to other elements of the program.

Note that if the unit system is set for metric, the conversion methods return input data unchanged.

The class provides methods for retrieving a string for the type of unit (e.g. "Power") and a label for the unit (e.g. "Watts"). The label is appropriate for the unit system currently selected.

The class extends the Observable class, giving a convenient means for updating objects that utilize it for conversions, when the unit system changes.


Field Summary
static int AREA
          Indicates that the type of unit is area.
static int ENERGY
          Indicates that the type of unit is energy.
static int IP
          Of choices METRIC or IP, indicates that English units are used.
static int LENGTH
          Indicates that the type of unit is length.
static int METRIC
          Of choices METRIC or IP, indicates that metric units are used.
static int NOTHING
          Indicates that no conversion will be made.
static int POWER
          Indicates that the type of unit is power.
static int POWER_PER_AREA
          Indicates that the type of unit is power per unit area.
static int RVALUE
          Indicates that the type of unit is R value.
static int TEMPERATURE
          Indicates that the type of unit is Temperature.
static int UVALUE
          Indicates that the type of unit is U value.
static int VELOCITY_FT_SEC
          Indicates that the type of unit is velocity.
static int VELOCITY_MPH
          Indicates that the type of unit is velocity.
 
Constructor Summary
UnitConverter(int unitType)
          Creates a UnitConverter object for conversion of unitType units.
 
Method Summary
 double getConversionFactor()
          Returns conversion factor (Metric to English).
 double getConvertedValue(double metric_value)
          Converts the passed value, assumed to be in metric, into the current unit system.
static double getConvertedValue(double metric_value, int unit_type)
           
 java.lang.String getLabel()
          Returns the current label associated with these units (e.g.
 double getMetricValue(double value)
          Converts the passed value, assumed to be in the current unit system, to metric and returns the value.
 java.lang.String getName()
          Returns the name of the units for which this object converts (e.g.
static int getUnitSystem()
           
 int getUnitType()
          Returns the type of unit for this converter.
static void setUnitSystem(int unitSystem)
          Sets the system of units to METRIC or IP.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

METRIC

public static final int METRIC
Of choices METRIC or IP, indicates that metric units are used.

IP

public static final int IP
Of choices METRIC or IP, indicates that English units are used.

NOTHING

public static final int NOTHING
Indicates that no conversion will be made.

TEMPERATURE

public static final int TEMPERATURE
Indicates that the type of unit is Temperature.

POWER_PER_AREA

public static final int POWER_PER_AREA
Indicates that the type of unit is power per unit area.

POWER

public static final int POWER
Indicates that the type of unit is power.

ENERGY

public static final int ENERGY
Indicates that the type of unit is energy.

RVALUE

public static final int RVALUE
Indicates that the type of unit is R value.

UVALUE

public static final int UVALUE
Indicates that the type of unit is U value.

AREA

public static final int AREA
Indicates that the type of unit is area.

VELOCITY_MPH

public static final int VELOCITY_MPH
Indicates that the type of unit is velocity.

VELOCITY_FT_SEC

public static final int VELOCITY_FT_SEC
Indicates that the type of unit is velocity.

LENGTH

public static final int LENGTH
Indicates that the type of unit is length.
Constructor Detail

UnitConverter

public UnitConverter(int unitType)
Creates a UnitConverter object for conversion of unitType units. for unitType, use one of the static class variables TEMPERATURE, POWER, POWER_PER_AREA, etc.
Method Detail

setUnitSystem

public static void setUnitSystem(int unitSystem)
Sets the system of units to METRIC or IP.

getUnitSystem

public static int getUnitSystem()

getUnitType

public int getUnitType()
Returns the type of unit for this converter.

getName

public java.lang.String getName()
Returns the name of the units for which this object converts (e.g. "Power").

getLabel

public java.lang.String getLabel()
Returns the current label associated with these units (e.g. "Watts").

getConvertedValue

public static double getConvertedValue(double metric_value,
                                       int unit_type)

getConvertedValue

public double getConvertedValue(double metric_value)
Converts the passed value, assumed to be in metric, into the current unit system.

getConversionFactor

public double getConversionFactor()
Returns conversion factor (Metric to English).

getMetricValue

public double getMetricValue(double value)
Converts the passed value, assumed to be in the current unit system, to metric and returns the value.