In this section, you will learn to convert Grams to
Pounds, Grams to Ounce, Kilograms to Pounds and Pounds to Kilograms. The following program helps you in converting one unit to another.
Code Description:
In this example we have taken a table of units as Grams, Kilograms, Pounds etc to convert them to another units. First of all it will ask to enter a choice then it will convert the unit. For the conversion process we have used
GramsToPounds();, GramsToOunce();, KilogramsToPounds(); and
PoundsToKilograms();. method. For the choice of entries we have used switch
case here. At last we have applied the formulae for all the conversions as shown in the
program.
We have also used conditional-OR operator to select the choice as 1,2 and so on. This means that the choice should be between 1 to 5 only. To take the input of the choice for TableChoice from the keyboard we have used the same method i.e Integer.parseInt(bf.readLine());.
import java.io.*;
System.exit(0);
|
Output of the program:
| C:\unique>javac UnitConverter.java C:\unique>java UnitConverter Conversion Table: ------------------------- 1. Grams To Pounds 2. Grams To Ounce 3. Kilograms To Pounds 4. Pounds To Kilograms 5. Quit ------------------------- Choice: 1 convertGramsToPounds Enter the Grams:1 Pound: 0.00220462262 Conversion Table: ------------------------- 1. Grams To Pounds 2. Grams To Ounce 3. Kilograms To Pounds 4. Pounds To Kilograms 5. Quit ------------------------- Choice: 4 convertPoundsToKilograms Enter the Pounds: 1 Kilo: 0.45 Conversion Table: ------------------------- 1. Grams To Pounds 2. Grams To Ounce 3. Kilograms To Pounds 4. Pounds To Kilograms 5. Quit ------------------------- Choice: 3 convertKilogramsToPounds Enter the Kilogram:1 Ponds: 2.20462262 Conversion Table: ------------------------- 1. Grams To Pounds 2. Grams To Ounce 3. Kilograms To Pounds 4. Pounds To Kilograms 5. Quit ------------------------- Choice: |
|
Recommend the tutorial |

Ask Questions? Discuss: Convert One Unit to Another
Post your Comment