Write java a program that converts some units. The program will convert followin
ID: 3841293 • Letter: W
Question
Write java a program that converts some units. The program will convert following units: Miles to Kilometers kilometer = miles x 1.609344 Pound to Kilogram kilogram = pound x 0.4536 Gallon to Liter liter = gallon x 3.7854 Your program should ask users to choose which unit he/she wants to convert. When he/she choose the option to convert, the program ask user to input unit and convert to unit he/she wants. For example, if he/she want to convert miles to kilometers, the program should ask user to input miles. Once the program gets miles, then it converts to the kilometers using above formula. All of the units uses decimal places. However, this program only allows two decimal places.
Explanation / Answer
Here is the code :
import java.util.*;
import java.text.*;
public class Conversion {
// conversion equivalencies
private static final double
KILOMETERS_PER_MILE = 1.609344;
private static final double
LITERS_PER_GALLON = 3.7854;
private static final double
KILOGRAM_PER_POUND= 0.4536;
// method to convert miles->kilometers
public static double MilesTokilometers(double miles) {
return miles* KILOMETERS_PER_MILE;
}
// method to convert Pound -> Kilogram
public static double PoundToKilograms(double pound) {
return pound * KILOGRAM_PER_POUND;
}
// method to convert gallons -> Liters
public static double gallonsToLiters(double gallons) {
return gallons * LITERS_PER_GALLON;
}
// function that takes the user input
public static double input(String msg){
System.out.println(msg);
Scanner stdin = new Scanner(System.in);
return stdin.nextDouble();
}
// function that outputs the result to two decimals
public static double roundoff(double val){
return Math.round(val*100.0)/100.0;
}
public static void main(String[] args) {
boolean flag=true;
double in;
while(flag){
System.out.println("1.Miles to Kilometers 2.Pound to Kilogram 3.Gallon to Liter 4.Exit");
double choice=input("Enter your Option:");
switch((int)choice){
case 1:
System.out.println("Output: " + roundoff(MilesTokilometers(input("Enter mile"))));
break;
case 2:
System.out.println("Output: "+ roundoff(PoundToKilograms(input("Enter pound"))));
break;
case 3:
System.out.println("Output: "+ roundoff(gallonsToLiters(input("Enter gallon"))));
break;
case 4:
flag=false;
break;
default:
System.out.println("wrong choice");
}
}
}
}
Sample output :
1.Miles to Kilometers 2.Pound to Kilogram 3.Gallon to Liter 4.Exit
Enter your Option:
1
Enter mile
14.5
Output: 23.34
1.Miles to Kilometers 2.Pound to Kilogram 3.Gallon to Liter 4.Exit
Enter your Option:
2
Enter pound
25.0
Output: 11.34
1.Miles to Kilometers 2.Pound to Kilogram 3.Gallon to Liter 4.Exit
Enter your Option:
3
Enter gallon
5.056
Output: 19.14
1.Miles to Kilometers 2.Pound to Kilogram 3.Gallon to Liter 4.Exit
Enter your Option:
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.