Volume.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program prints the price per liter for a six-pack of cans and
  5     a two-liter bottle.
  6  */
  7  public class Volume
  8  {
  9     public static void main(String[] args)
 10     {
 11        // Read price per pack
 12  
 13        Scanner in = new Scanner(System.in);
 14  
 15        System.out.print("Please enter the price for a six-pack: ");
 16        double packPrice = in.nextDouble();
 17  
 18        // Read price per bottle
 19  
 20        System.out.print("Please enter the price for a two-liter bottle: ");
 21        double bottlePrice = in.nextDouble();
 22  
 23        final double CANS_PER_PACK = 6;
 24        final double CAN_VOLUME = 0.355; // 12 oz. = 0.355 l 
 25        final double BOTTLE_VOLUME = 2;
 26  
 27        // Compute and print price per liter
 28  
 29        double packPricePerLiter = packPrice / (CANS_PER_PACK * CAN_VOLUME);
 30        double bottlePricePerLiter = bottlePrice / BOTTLE_VOLUME;
 31  
 32        System.out.printf("Pack price per liter:   %8.2f", packPricePerLiter);
 33        System.out.println();
 34        
 35        System.out.printf("Bottle price per liter: %8.2f", bottlePricePerLiter);
 36        System.out.println();
 37     }
 38  }