A Simple Java Measurement Utility Library (i.e., lb to kg, in to cm)

While I was writing an EMR application, I quickly realized that my framework needed a class with static methods to help me perform measurement conversions. Namely I needed to convert pounds to kilograms and inches to centimeters.

I searched the web for a while and didn't find a stock Jakarta Commons utility, so I decided I would build my own class named MeasurementUtil. In the future, when I come up with a new measurement conversion requirement it will be placed within this class in my framework package. You'll probably look at my code and say, "well that’s extremely simple, why not just do calculation using the logic inline". The answer is simple: I wanted a basic utility class that encapsulates all of my measurement conversion logic. In reality this could probably be turned into its own package with a suite of classes, but since I only started off with the requirement for two measurement conversions, we ended up with one class for now. This is in an effort to keep it simple.

Now, while the class I will present below is simple, I hope that it saves you a few minutes when writing your application. Converting pounds to kilograms was a need that I shouldn’t have had to waste 20 minutes looking up and quickly writing a class to do it.

And now we present the class. Drum roll please…..

/**
 * Copyright (c) 2002-2004 by Timothy E. Archer.  All rights reserved.
 *
 * $Id: MeasurementUtil.java,v 1.1 2004/07/07 02:55:03 tima Exp $
 *
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL TIMOTHY E. ARCHER BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
package com.tima.util;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.apache.log4j.*;

/**
 * General measurement conversion utilities/helper methods.
 *
 * @author  Tim Archer 10/28/04
 * @version $Revision: 1.1 $
 */
public class MeasurementUtil {
    /** The number of kilograms per pound. */
    public static final double POUND_PER_KG = 2.20462262;
    /** The number of centimeters per inch. */
    public static final double CM_PER_INCH = 2.54;
    
    /**
     * Convert kilograms to pounds.
     *
     * @param kilograms The kilograms measurement
     * @return double The kilograms converted to pounds.
     */
    public static double kilogramsToPounds (double kilograms) {
        return kilograms * POUND_PER_KG;
    }
    
    /**
     * Convert pounds to kilograms.
     *
     * @param pounds The pounds measurement
     * @return double The pounds converted to kilograms.
     */
    public static double poundsToKilograms (double pounds) {
        return pounds / POUND_PER_KG;
    }    
    
    /**
     * Convert centimeters to inches.
     *
     * @param centimeters The centimeters measurement
     * @return double The centimeters converted to inches.
     */
    public static double centimetersToInches (double centimeters) {
        return centimeters / CM_PER_INCH;
    }    
    
    /**
     * Convert inches to centimeters.
     *
     * @param inches The inches measurement
     * @return double The inches converted to centimeters.
     */
    public static double inchesToCentimeters (double inches) {
        return inches * CM_PER_INCH;
    }    
    
    /**
     * Main - Main unit test procedure
     * @param args Command line arguments.
     *
     */
    public static void main(String args[]) {
	try {
          System.out.println("Measurement util starting.");
          double pounds = 250.0;
          double kg = MeasurementUtil.poundsToKilograms(pounds);
          System.out.println("Pounds: "+pounds+" KG: "+kg);
          
          double inches = 60.0;
          double cm = MeasurementUtil.inchesToCentimeters(inches);
          System.out.println("Inches: "+inches+" CM: "+cm);
          
        } catch (Exception e) {
          System.err.println("An error occurred:"+e.toString());
        }
    }        
}



The main method contains example usages of the poundsToKilograms and inchesToCentimeters methods.
The key code lines are:

double kg = MeasurementUtil.poundsToKilograms(pounds);
double cm = MeasurementUtil.inchesToCentimeters(inches);


v2.0