Answer to Question #303764 in Java | JSP | JSF for ndfd

Question #303764

Write a program that uses one or more if statements to solve the following problem:


Prompt the user for a number (type double) and the type of conversion calculation to apply. Once the user supplies the value and the type of conversion, your program should apply the appropriate conversion and display the result. For the conversion type, the user will enter one of the following conversion conversion codes: GL, IC, PK, MK.


Based on the user's input, your program will compute and display the appropriate result. See below for the meaning of the codes:


GL = Gallon to Liter

IC = Inch to Centimeter

PK = Pound to Kilogram

MK = Mile to Kilometer


For instance, if the user inputs GL as their conversion type, the program will then take their numeric input as gallons and converrt it to liters. The liters value would then be output to the user. Please find some additional information below to assist you in solving this problem:





1
Expert's answer
2022-02-28T16:21:26-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number:");
        double number = in.nextDouble();
        System.out.println("Enter the conversion code(GL, IC, PK, MK):");
        String conversionCode = in.next();
        if (conversionCode.equals("GL")) {
            System.out.println(number * 3.785 + " liters.");
        } else if (conversionCode.equals("IC")) {
            System.out.println(number * 2.54 + " centimeters.");
        } else if (conversionCode.equals("PK")) {
            System.out.println(number / 2.205 + " kilograms.");
        } else if (conversionCode.equals("MK")) {
            System.out.println(number * 1.609 + " kilometers.");
        }
    }
}

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
APPROVED BY CLIENTS