distanceunit.java

来自「Ftp服务1.0」· Java 代码 · 共 57 行

JAVA
57
字号
package ranab.unit;

/**
 * Distance unit converter class.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class DistanceUnit extends BaseUnit {
    
    private final static String[] UNITS = {
        "inch", 
        "foot",    
        "yard",   
        "mile", 
        "millimeter", 
        "centimeter", 
        "meter", 
        "kilometer"
    }; 
    
    // first convert it to meter (the base unit)
    private final static double[] METER_TABLE = {
        39.37007874015748,
        3.280839895013123,
        1.0936132983377078,
        0.0006213711922373339,
        1000,
        100,
        1,
        0.001    
    };
    
    /**
     * Get all the possible units.
     */
    public String[] getAllUnits() {
        return UNITS;
    }
    
    /**
     * Convert one unit from another
     */
    public String convertUnit(String value, int from, int to) throws UnitException {
        double inVal = getDoubleValue(value);
        double outVal = inVal*METER_TABLE[to]/METER_TABLE[from];
        return String.valueOf(outVal);
    }
    
    /**
     * Return unit type.
     */
    public String toString() {
        return "Distance";
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?