Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a JAVA program for converting US measurement values to their corresponding

ID: 3834115 • Letter: W

Question

Write a JAVA program for converting US measurement values to their corresponding metric measurement.

The program will have at a minimum 7 methods as follows:

The main method of the program will make calls to the various methods required for this assignment.   It will contain a loop so that multiple conversions may be performed in one run of the program. The main program evaluates the choice of conversion to perform, prompts for the value to convert and calls the appropriate method which returns the converted value. It then outputs the conversion performed and the from to values with appropriate labels.

A method that displays the menu options for which conversion to perform. The main method prompts for entry of the menu choice.

A Miles to Kilometers method that requires the number of miles to convert as a parameter. US Miles / 0.62137 gives equivalent kilometer conversion.

A Dollars to Euros method that requires the dollar and cents amount to convert as a parameter. US Dollars / 0.938082 gives equivalent Euro conversion.

A Fluid ounces to Milliliters method that requires the number of fluid ounces to convert as a parameter. US Fluid Ounces / 0.033814 gives equivalent Milliliter conversion.

A Gallon to Liters method that requires the number of gallons to convert as a parameter. US Gallons / 0.26417 gives equivalent Liter conversion.

A Pounds to Kilograms method that requires the number of pounds to convert as a parameter. US Pounds / 2.2046 gives equivalent kilogram conversion.

For output, the program needs to display the conversion being perform and the actual from and converted values.

(eg.)

US to Metric Measurement Conversion

1) Miles to Kilometers

2) Dollars to Euros

3) Fluid ounces to Milliliters

4) Gallons to Liters

5) Pounds to Kilograms

6) Exit the program

Enter your conversion choice: 1

Converting Miles to Kilometers

2.5 miles is 4.02336772 kilometers

To continue enter another conversion choice:

Explanation / Answer

Here is the code for you:

import java.util.*;
class USToMetric
{
    //The main method of the program will make calls to the various methods required for
    //this assignment. It will contain a loop so that multiple conversions may be
    //performed in one run of the program.
    //The main program evaluates the choice of conversion to perform, prompts for the
    //value to convert and calls the appropriate method which returns the converted value.
    //It then outputs the conversion performed and the from to values with appropriate labels.
    public static void main(String[] args)
    {
       while(true)
       {
          int choice = menuChoice();
          double input;
          Scanner sc = new Scanner(System.in);
          switch(choice)
          {
             case 1:   System.out.print("Enter the miles: ");
                     input = sc.nextDouble();
                     System.out.println(input + " miles = " + milesToKms(input) + " kms.");
                     break;
             case 2:   System.out.print("Enter Dollars: ");
                     input = sc.nextDouble();
                     System.out.println(input + " dollars = " + dollarsToEuros(input) + " Euros.");
                     break;
             case 3: System.out.print("Enter Ounces: ");
                     input = sc.nextDouble();
                     System.out.println(input + " ounces = " + ouncesToMilliliters(input) + " ml.");
                     break;
             case 4:   System.out.print("Enter gallons: ");
                     input = sc.nextDouble();
                     System.out.println(input + " gallons = " + gallonsToLiters(input) + " liters.");
                     break;
             case 5:   System.out.print("Enter pounds: ");
                     input = sc.nextDouble();
                     System.out.println(input + " pounds = " + poundsToKilograms(input) + " kilograms.");
                     break;
             case 6:   return;
          }
       }
    }
    //A method that displays the menu options for which conversion to perform.
    //The main method prompts for entry of the menu choice.
    public static int menuChoice()
    {
       System.out.println("US to Metric Measurement Conversion");
       System.out.println("1) Miles to Kilometers");
       System.out.println("2) Dollars to Euros");
       System.out.println("3) Fluid ounces to Milliliters");
       System.out.println("4) Gallons to Liters");
       System.out.println("5) Pounds to Kilograms");
       System.out.println("6) Exit the program");
       System.out.print("Enter your conversion choice: ");
       Scanner sc = new Scanner(System.in);
       int choice = sc.nextInt();
       while(choice < 1 || choice > 6)
       {
          System.out.println("Invalid choice. Please enter a value in the range 1 - 6.");
          System.out.print("Enter your conversion choice: ");
          choice = sc.nextInt();
       }                  
       return choice;
    }
   
    //A Miles to Kilometers method that requires the number of miles to convert as a parameter.
    //US Miles / 0.62137 gives equivalent kilometer conversion.
    public static double milesToKms(double miles)
    {
       return miles / 0.62137;
    }
   
    //A Dollars to Euros method that requires the dollar and cents amount to convert as a parameter.
    //US Dollars / 0.938082 gives equivalent Euro conversion.
    public static double dollarsToEuros(double dollars)
    {
       return dollars / 0.938082;
    }
   
    //A Fluid ounces to Milliliters method that requires the number of fluid ounces to convert as a parameter.
    //US Fluid Ounces / 0.033814 gives equivalent Milliliter conversion.
    public static double ouncesToMilliliters(double ounces)
    {
       return ounces / 0.033814;
    }
   
    //A Gallon to Liters method that requires the number of gallons to convert as a parameter.
    //US Gallons / 0.26417 gives equivalent Liter conversion.
    public static double gallonsToLiters(double gallons)
    {
       return gallons / 0.26417;
    }
   
    //A Pounds to Kilograms method that requires the number of pounds to convert as a parameter.
    //US Pounds / 2.2046 gives equivalent kilogram conversion.
    public static double poundsToKilograms(double pounds)
    {
       return pounds / 2.2046;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote