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

The program that follows is a unit conversion program that asks the users: 1) fr

ID: 3698782 • Letter: T

Question

The program that follows is a unit conversion program that asks the users:


1) from which unit they want to convert (fl. oz, gal, oz, lb, in, ft, mi) and
2) to which unit they want to convert (ml, l, g, kg, mm, cm, m, km).

For example:

Convert from? gal
Convert to? ml
Value? 2.5
2.5 gal = 9462.5 ml

The code has many errors and omissions - both compiler and run-time problems. Your job is to fix them. Work in teams of three or four as assigned by your instructor.

java.util.Scanner;

public class UnitConversion

{

   public static void main(String[] args)

   {

      final double ML_PER_FL_OZ = 29.5735296;

      final double ML_PER_GAL = 3785.41178;

            double G_PER_OZ = 28.3495231;

      final double          = 453.59237;

      final double M_PER_IN = 0.0254

      final        M_PER_FT = 0.3048;

      final double M_PER_MI = 1609.344;

      Scanner    = new Scanner(System.in);

      System.out.print("Convert from? ");

      String fromUnit = in.next();

      system.out.print("Convert to? ");

      String toUnit = in.next();

           lastChar = toUnit.charAt(toUnit.length() - 1);

      if (fromUnit.compareTo("fl. oz") == 0)

      {

         // fl. oz to ..

         if (lastChar == 'l')

         {

            // fl oz to mililiters

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value * ML_PER_FL_OZ;

            if (toUnit.charAt( ) == 'l')

            {

               // fl oz to liters

               conversion /= 1000;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.     ("Incompatible Units.");

         }

      }

      if (        .compareTo("gal") == 0)

      {

         // Gallons to ...

         if (lastChar == 'l')

         {

            // Gallons to mililiters

            System.out.print("Value? ");

            double value =              ;

            double conversion = value * ML_PER_GAL;

            if (toUnit.charAt(0) == 'l')

            {

               // Gallons to liters

               conversion /= 1000;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.println("Incompatible Units.");

         }

      }

      if (fromUnit.compareTo("oz") == 0)

      {

         // oz to ...

         if (lastChar == 'g')

         {

            // oz to grams

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value *       ;

            if (toUnit.charAt(0) = 'k')

            {

               // oz to kilograms

               conversion /= 1000;

            }

            System.out.printf("%f %s = %f %s ", value,       , conversion, toUnit);

         }

         else

         {

            System.out.println("Incompatible Units.");

         }

      }

      if (fromUnit.compareTo("lb") == 0)

      {

         // lb to ...

         if (lastChar == 'g')

         {

            // lb to grams

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value * G_PER_LB;

            if (toUnit.charAt(0) == 'k')

            {

               // lb to kilograms

               conversion /= 1000;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.println("Incompatible Units.");

         }

      }

      if (fromUnit.compareTo("in") == 0)

      {

         // in to ...

         if (lastChar == 'm')

         {

            // in to m

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value * M_PER_IN;

            if (toUnit.compareTo("km") == 0)

            {

               // in to kilometers

               conversion /= 1000;

            }

            if (toUnit.compareTo("cm") == 0)

            {

               // in to centimeters

               conversion *= 100;

            }

            if (toUnit.compareTo("mm") == 0)

            {

               // in to milimeters

               conversion *= 1000;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.println("Incompatible Units.");

         }

      }

      if (fromUnit.compareTo("ft") == 0)

      {

         // ft to ...

         if (             )

         {

            // ft to meters

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value * M_PER_FT;

            if (toUnit.compareTo("km") == 0)

            {

               // ft to kilometers

               conversion /= 1000;

            }

            if (toUnit.compareTo("cm") == 0)

            {

               // ft to centimeters

               conversion *= 100;

            }

            if (toUnit.          ("mm") == 0)

            {

               // ft to milimeters

               conversion *= 1000;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.println("Incompatible Units.");

         }

      }

      if (fromUnit.compareTo("mi") == 0)

      {

         // m to ...

         if (lastChar == 'm')

         {

            // mi to m

            System.out.print("Value? ");

            double value = in.nextDouble();

            double conversion = value * M_PER_MI;

            if (toUnit.compareTo("km") == 0)

            {

               // mi to kilometers

               conversion /= 1000;

            }

            if (toUnit.compareTo(" ") == 0)

            {

               // mi to centimeters

               conversion *= 100;

            }

            if (toUnit.compareTo("mm") == 0)

            {

               // mi to milimeters

               conversion *= 1;

            }

            System.out.printf("%f %s = %f %s ", value, fromUnit, conversion, toUnit);

         }

         else

         {

            System.out.println(“           ");

         }

      }

  

}

Explanation / Answer

UnitConversion.java

import java.util.Scanner;

public class UnitConversion

{

public static void main(String[] args)

{

final double ML_PER_FL_OZ = 29.5735296;

final double ML_PER_GAL = 3785.41178;

double G_PER_OZ = 28.3495231;

final double G_PER_LB = 453.59237;

final double M_PER_IN = 0.0254;

final double M_PER_FT = 0.3048;

final double M_PER_MI = 1609.344;

Scanner in = new Scanner(System.in);

System.out.print("Convert from? ");

String fromUnit = in .next();

System.out.print("Convert to? ");

String toUnit = in .next();

char lastChar = toUnit.charAt(toUnit.length() - 1);

if (fromUnit.compareTo("fl. oz") == 0)

{

// fl. oz to ..

if (lastChar == 'l')

{

// fl oz to mililiters

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * ML_PER_FL_OZ;

if (toUnit.charAt(0) == 'l')

{

// fl oz to liters

conversion /= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("gal") == 0)

{

// Gallons to ...

if (lastChar == 'l')

{

// Gallons to mililiters

System.out.print("Value? ");

double value = in .nextDouble();;

double conversion = value * ML_PER_GAL;

if (toUnit.charAt(0) == 'l')

{

// Gallons to liters

conversion /= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("oz") == 0)

{

// oz to ...

if (lastChar == 'g')

{

// oz to grams

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * G_PER_OZ;

if (toUnit.charAt(0) == 'k')

{

// oz to kilograms

conversion /= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("lb") == 0)

{

// lb to ...

if (lastChar == 'g')

{

// lb to grams

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * G_PER_LB;

if (toUnit.charAt(0) == 'k')

{

// lb to kilograms

conversion /= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("in") == 0)

{

// in to ...

if (lastChar == 'm')

{

// in to m

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * M_PER_IN;

if (toUnit.compareTo("km") == 0)

{

// in to kilometers

conversion /= 1000;

}

if (toUnit.compareTo("cm") == 0)

{

// in to centimeters

conversion *= 100;

}

if (toUnit.compareTo("mm") == 0)

{

// in to milimeters

conversion *= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("ft") == 0)

{

// ft to ...

if (lastChar == 'm')

{

// ft to meters

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * M_PER_FT;

if (toUnit.compareTo("km") == 0)

{

// ft to kilometers

conversion /= 1000;

}

if (toUnit.compareTo("cm") == 0)

{

// ft to centimeters

conversion *= 100;

}

if (toUnit.compareTo("mm") == 0)

{

// ft to milimeters

conversion *= 1000;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

if (fromUnit.compareTo("mi") == 0)

{

// m to ...

if (lastChar == 'm')

{

// mi to m

System.out.print("Value? ");

double value = in .nextDouble();

double conversion = value * M_PER_MI;

if (toUnit.compareTo("km") == 0)

{

// mi to kilometers

conversion /= 1000;

}

if (toUnit.compareTo("cm") == 0)

{

// mi to centimeters

conversion *= 100;

}

if (toUnit.compareTo("mm") == 0)

{

// mi to milimeters

conversion *= 1;

}

System.out.printf("%f %s = %f %s ", value, fromUnit,
conversion, toUnit);

} else

{

System.out.println("Incompatible Units.");

}

}

}
}

___________________

Output:

Convert from? gal
Convert to? l
Value? 10
10.000000 gal = 37.854118 l

_______________Thank You