0

I have taken this code from an example but I don't know how to calculate the value to Base Unit. This is the code for converting energy:

public enum U_EnergyConverter implements UnitConverter {
energy_calories {
    @Override
    public double toBaseUnit(double amount) {
        return amount;
    }

},
energy_joules {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 4.19;
    }
},
energy_kilocalories {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 0.001;
    }

};
public abstract double toBaseUnit(double amount);

public double toUnit(double baseUnitAmount) {
    return baseUnitAmount * (1 / (toBaseUnit(1)));
}

The Amounts I used in the code above are giving me wrong result. And I don't know from which source I can get the exact value for unit-conversion!!

Appreciate any help

Update: This is the mass unit code which is working very well:

public enum MassConverter implements UnitConverter {
mass_g {
    @Override
    public double toBaseUnit(double amount) {
        return amount;
    }

},
mass_kg {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 1000.00;
    }

},
mass_oz {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 28.3495231;
    }

},
mass_lb {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 453.59237;
    }

};
public abstract double toBaseUnit(double amount);

public double toUnit(double baseUnitAmount) {
    return baseUnitAmount * (1 / (toBaseUnit(1)));
}
swis.m
  • 61
  • 1
  • 8
  • 1
    I don't understand your question. `The Amounts I used are not giving me the right result.` be more explicit. give specific examples. – njzk2 May 06 '14 at 14:23
  • for example when I convert the 15 kilo calories to calories its giving me 62802.0 which is wrong it suppose to give me 15000 calories. – swis.m May 06 '14 at 14:27
  • post the code you use to obtain that result – njzk2 May 06 '14 at 14:30
  • sorry I meant the wrong result. – swis.m May 06 '14 at 14:31
  • First code what I posted above giving me wrong result. but the second code working well. – swis.m May 06 '14 at 14:34
  • no, post the actual code you use to obtain 62802.0. (probably something along the lines of `U_EnergyConverter.energy_kilocalories.toBaseUnit(15)`?) – njzk2 May 06 '14 at 14:36

1 Answers1

1

Change like this:

energy_kilocalories {
    @Override
    public double toBaseUnit(double amount) {
        return amount * 1000;
    }

};

toBaseUnit should return value in calories given the value in those units (such as joules, kilocalories etc). This is what the correct example does. Yours does the opposite.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158