Answer to Question #279567 in Java | JSP | JSF for shreckz

Question #279567

Write a java program to convert car consumption from one







unit to another. Offer the user the choice of either







conversion, kpl to mpg or mpg to kpl. The interface







should be:







PREFERRED CONVERSION:







1. kpl to mpg







2. mpg to kpl







Enter your choice: 2







Enter miles per gallon value: mpg_value







The equivalent kilometers per liter is: kpl_value







Use the constants 1 mile = 1609 meters and 1 gallon = 3.785







liters.







Useful formula:







mpg = (kpl / 1.609) * 3.785







kpl = (mpg * 1.609) / 3.785








note: use switch control statement






1
Expert's answer
2021-12-14T21:33:57-0500
import java.util.Scanner;

public class Main {
    public static final double KILOMETERS_IN_MILE = 1.609;
    public static final double LITERS_IN_GALLON = 3.785;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("PREFERRED CONVERSION:");
        System.out.println("1. kpl to mpg");
        System.out.println("2. mpg to kpl");
        System.out.print("Enter your choice: ");
        String choice = in.nextLine();
        double mpgValue;
        double kplValue;
        switch (choice) {
            case "1":
                System.out.print("Enter kilometers per liter value: ");
                kplValue = Double.parseDouble(in.nextLine());
                mpgValue = (kplValue / KILOMETERS_IN_MILE) * LITERS_IN_GALLON;
                System.out.println("The equivalent miles per gallon is: " + mpgValue);
                break;
            case "2":
                System.out.print("Enter miles per gallon value: ");
                mpgValue = Double.parseDouble(in.nextLine());
                kplValue = (mpgValue * KILOMETERS_IN_MILE) / LITERS_IN_GALLON;
                System.out.println("The equivalent kilometers per liter is: " + kplValue);
                break;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog