Again in this section, you will learn to convert One
Unit to Another. The following program helps you in converting some more units
to another.
Code Description:
In this example we have taken a table of units as Gallons, Liters, Miles,
Carats, Meters 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 convertGallonsToLtrs();, convertMetersToMiles();,
convertLtrsToGallons();, convertCaratsToGrams();
etc. 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());.
Here is the code of this program:
|
Output of the program:
| C:\unique>javac UnitConverter1.java C:\unique>java UnitConverter1 Conversion Table: 1. Gallons To Liters 2. Meters To Miles 3. Grams To Carats 4. Inches To Meters 5. Liters To Gallons 6. Grams To kilograms 7. Carats To Grams 8. Quit Choice: 1 convertGallonsToLtrs Enter the Gallon:1 Liters: 3.7854118 Conversion Table: 1. Gallons To Liters 2. Meters To Miles 3. Grams To Carats 4. Inches To Meters 5. Liters To Gallons 6. Grams To kilograms 7. Carats To Grams 8. Quit Choice: 2 convertMetersToMiles Enter the Meters:1 Miles: 6.2137119E-4 Conversion Table:
1. Gallons To Liters
Choice: 5
1. Gallons To Liters |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Convert One Unit to Another
Post your Comment