Science has lot of units. Some of the units are converted into another unit. For example, temperature has Celsius and Fahrenheit. Distance has miles and kilometres.
Let us
create java program to convert these units.
Program implementation:
- · This program reads the choice from the user. Based on the choice,the conversion starts.
- · The input value is read from the command line.
- · If the choice is 1, it converts the Celsius value to Fahrenheit value and print it.
- · If the choice is 2, it makes the Fahrenheit value to Celsius value and display it.
- · If the choice value is 3, the program creates the conversion of kilometers into miles.
- · If the choice is 4, the program converts the miles value into kilometer value.
- · If choice is any other, it displays the message “Please choose the option between 1 to 4”.
Program:
import java.util.Scanner;
public class convertUnit {
public static void
main(String[] args) {
Scanner
scanner = new Scanner(System.in);
System.out.println("Unit Converter");
System.out.println("Choose an option:");
System.out.println("1.Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.println("3. Kilometers to Miles");
System.out.println("4. Miles to Kilometers");
System.out.println("Enter your Choice");
int choice =
scanner.nextInt();
System.out.println("Enter the value:");
System.out.println("");
double value =
scanner.nextDouble();
double cValue;
if (choice
==1) {
//Convert
Celsius to Fahrenheit
double F =
(value * 9/5) + 32;
System.out.println("The converted value of Celsius is: "+ F
+"Fahrenheit");
}
else if
(choice == 2){
//Convert
Fahrenheit to Celsius
double
C=(value-32)/1.8;
System.out.println("The converted value of Fahrenheit is:
"+C+"Celsius");
}
else if
(choice == 3) {
// Convert
Kilometers to Miles
cValue =
value * 0.621371;
System.out.println("The converted value of Kilometers
is:"+cValue+" Miles.");
}
else if (choice
== 4) {
// Convert
Miles to Kilometers
cValue =
value / 0.621371;
System.out.println("The converted value of miles:" + cValue +
" Kilometers.");
}
else {
System.out.println("Invalid choice. Please select option between 1
to 4.");
}
scanner.close();
}
}
Output:
Compile and run the program to get the output.
C:\raji\blog>javac convertUnit.java
C:\raji\blog>java
convertUnit Unit
Converter Choose an
option: 1.Celsius to
Fahrenheit 2. Fahrenheit
to Celsius 3. Kilometers
to Miles 4. Miles to Kilometers Enter your
Choice 1 Enter the
value: 98 The converted
value of Celsius is: 208.4Fahrenheit |
C:\raji\blog>java
convertUnit Unit
Converter Choose an
option: 1.Celsius to
Fahrenheit 2. Fahrenheit
to Celsius 3. Kilometers
to Miles 4. Miles to Kilometers Enter your
Choice 2 Enter the
value: 200 The converted
value of Fahrenheit is: 93.33333333333333Celsius |
C:\raji\blog>java
convertUnit Unit
Converter Choose an
option: 1.Celsius to
Fahrenheit 2. Fahrenheit
to Celsius 3. Kilometers
to Miles 4. Miles to Kilometers Enter your
Choice 3 Enter the
value: 34 The converted
value of Kilometers is:21.126614 Miles. |
C:\raji\blog>java
convertUnit Unit
Converter Choose an
option: 1.Celsius to
Fahrenheit 2. Fahrenheit
to Celsius 3. Kilometers
to Miles 4. Miles to Kilometers Enter your
Choice 4 Enter the
value: 56 The converted
value of miles:90.12329188198355 Kilometers. |
This is the way of implementing unit conversion program in
java.Hope this code will useful to you. Keep coding….
No comments:
Post a Comment