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.
Convert One Unit to Another
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:
import java.io.*;
import java.util.*;
public class UnitConverter1{
BufferedReader bf;
public static void main (String[] args) throws Exception{
UnitConverter1 Un = new UnitConverter1();
}
public UnitConverter1() throws Exception{
bf = new BufferedReader(new InputStreamReader(System.in));
int TableChoice;
boolean done = false;
while (!done) {
TableChoice = runMenu();
switch (TableChoice) {
case 1: System.out.println("convertGallonsToLtrs");
convertGallonsToLtrs();
break;
case 2: System.out.println("convertMetersToMiles");
convertMetersToMiles();
break;
case 3: System.out.println("convertGramsToCarats");
convertGramsToCarats();
break;
case 4: System.out.println("convertInchesToMtrs");
convertInchesToMtrs();
break;
case 5: System.out.println("convertLtrsToGallons");
convertLtrsToGallons();
break;
case 6: System.out.println("convertGmsTokgs");
convertGmsTokgs();
break;
case 7: System.out.println("convertCaratsToGrams");
convertCaratsToGrams();
break;
default: System.out.println("Quiting...");
System.exit(0);
break;
}
}
}
public int runMenu() throws Exception{
int TableChoice;
System.out.println();
System.out.println("Conversion Table:");
System.out.println("-------------------------");
System.out.println("1. Gallons To Liters");
System.out.println("2. Meters To Miles");
System.out.println("3. Grams To Carats");
System.out.println("4. Inches To Meters");
System.out.println("5. Liters To Gallons");
System.out.println("6. Grams To kilograms");
System.out.println("7. Carats To Grams");
System.out.println("8. Quit");
System.out.println("-------------------------");
System.out.print("Choice: ");
TableChoice = Integer.parseInt(bf.readLine());
while (TableChoice < 1 || TableChoice > 8) {
System.out.print("Invalid choice, try again: ");
TableChoice = Integer.parseInt(bf.readLine());
}
System.out.println();
return TableChoice;
}
public void convertGallonsToLtrs() throws Exception{
System.out.print("Enter the Gallon:");
int gallon = Integer.parseInt(bf.readLine());
double liters = gallon * 3.7854118;
System.out.println("Liters: " + liters);
}
public void convertMetersToMiles() throws Exception{
System.out.print("Enter the Meters:");
int meter = Integer.parseInt(bf.readLine());
double miles = meter * 0.00062137119;
System.out.println("Miles: " + miles);
}
public void convertGramsToCarats() throws Exception{
System.out.print("Enter the Gram:");
int gram = Integer.parseInt(bf.readLine());
double carats = gram * 5;
System.out.println("Carats: " + carats);
}
public void convertInchesToMtrs() throws Exception{
System.out.print("Enter the Inches:");
int inches = Integer.parseInt(bf.readLine());
double meters = inches * 0.0254;
System.out.println("Meters:" + meters);
}
public void convertLtrsToGallons() throws Exception{
System.out.print("Enter the Liters:");
int liter = Integer.parseInt(bf.readLine());
double gallon = liter * 0.264172052358148;
System.out.println("Gallons: " + gallon);
}
public void convertGmsTokgs() throws Exception{
System.out.print("Enter the Grams:");
int gram = Integer.parseInt(bf.readLine());
double kilogram = gram * 0.001;
System.out.println("kilogram: " + kilogram);
}
public void convertCaratsToGrams() throws Exception{
System.out.print("Enter the Carat:");
int carat = Integer.parseInt(bf.readLine());
double gram = carat * 0.2;
System.out.println("Gram:" + gram);
}
}
|
|
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
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: 3
convertGramsToCarats
Enter the Gram:1
Carats: 5.0
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: 4
convertInchesToMtrs
Enter the Inches:1
Meters:0.0254
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: 5
convertLtrsToGallons
Enter the Liters:1
Gallons: 0.264172052358148
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: 6
convertGmsTokgs
Enter the Grams:1
kilogram: 0.0010
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: 7
convertCaratsToGrams
Enter the Carat:1
Gram:0.2
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: 8
Quiting...
C:\unique>
|
Download this example.