Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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( ");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe how to get and give criticism effectively.

Answered: 1 week ago