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

Keeping track of and converting between US fluid quantities can be quite confusi

ID: 654161 • Letter: K

Question

Keeping track of and converting between US fluid quantities can be quite confusing and challenging, given all those teaspoons, ounces, quarts, etc. The relationships between all of these fluid units are:

1 fluid ounce = 6 teaspoons 1 gill = 4 fluid ounces
1 cup = 2 gills
1 pint = 2 cups

1 quart = 2 pints
1 gallon = 4 quarts
1 hogshead = 63 gallons

Write a program that asks the user to input a number of fluid ounces. Allocate the fluid ounces into the various units, first starting with hogsheads, then gallons, and so on, finishing with teaspoons.

Two sample runs follow (user input is in red).

Sample 1:

Sample 2:

Explanation / Answer

import java.util.Scanner;

public class P3_24 {
public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
   final double FLUID_OUNCE_TO_LITER = 0.029586;
   final double GALLON_TO_LITER = 3.758;
   final double OUNCE_TO_GRAM = 28.3495;
   final double POUND_TO_GRAM = 453.6;
   final double INCH_TO_METER = 0.00254;
   final double FOOT_TO_METER = 0.305;
   final double MILE_TO_METER = 1609.0;
   System.out.print("Convert from? (fl.oz, gal, oz, lb, in, ft, mi) ");
   String convert_from = input.next();
   System.out.print("Convert to? (ml, l, g, kg, mm, cm, m, km) ");
   String convert_to = input.next();
   System.out.print("Value: ");
   double value = input.nextDouble();
   input.close();
  
   double c_factor = 0.0;
  
   if (convert_from.equals("fl.oz")) {
   if (convert_to.equals("ml")) {
       c_factor = FLUID_OUNCE_TO_LITER * 1000;
   }
   else if (convert_to.equals("l")) {
       c_factor = FLUID_OUNCE_TO_LITER;
   }
   }
   else if (convert_from.equals("gal")) {
   if (convert_to.equals("ml")) {
       c_factor = GALLON_TO_LITER * 1000;
   }
   else if (convert_to.equals("l")) {
       c_factor = GALLON_TO_LITER;
   }
   }
   else if (convert_from.equals("oz")) {
   if (convert_to.equals("g")) {
       c_factor = OUNCE_TO_GRAM;
   }
   else if (convert_to.equals("kg")) {
       c_factor = OUNCE_TO_GRAM / 1000;
   }
   }
   else if (convert_from.equals("lb")) {
   if (convert_to.equals("g")) {
       c_factor = POUND_TO_GRAM;
   }
   else if (convert_to.equals("kg")){
       c_factor = POUND_TO_GRAM / 1000;
   }
   }
   else if (convert_from.equals("in")) {
   if (convert_to.equals("mm")) {
       c_factor = INCH_TO_METER * 1000;
   }
   else if (convert_to.equals("cm")) {
       c_factor = INCH_TO_METER * 100;
   }
   else if (convert_to.equals("m")) {
       c_factor = INCH_TO_METER;
   }
   else if (convert_to.equals("km")) {
       c_factor = INCH_TO_METER / 1000;
   }
   }
   else if (convert_from.equals("ft")) {
   if (convert_to.equals("mm")) {
       c_factor = FOOT_TO_METER * 1000;
   }
   else if (convert_to.equals("cm")) {
       c_factor = FOOT_TO_METER * 100;
   }
   else if (convert_to.equals("m")) {
       c_factor = FOOT_TO_METER;
   }
   else if (convert_to.equals("km")) {
       c_factor = FOOT_TO_METER / 1000;
   }
   }
   else if (convert_from.equals("ml")) {
   if (convert_to.equals("mm")) {
       c_factor = MILE_TO_METER * 1000;
   }
   else if (convert_to.equals("cm")) {
       c_factor = MILE_TO_METER * 100;
   }
   else if (convert_to.equals("m")) {
       c_factor = MILE_TO_METER;
   }
   else if (convert_to.equals("km")) {
       c_factor = MILE_TO_METER / 1000;
   }
   }
  
   if (c_factor == 0.0) {
   System.out.println("Conversion is unavailable.");
   }
   else {
   double converted = value * c_factor;
   System.out.printf("%.4f %s = %.4f %s", value, convert_from, converted, convert_to);
   }
}
}