PriceDemo.java.html

  1  /**
  2     This program computes the price per liter of a six-pack of soda
  3     cans.
  4  */
  5  public class PriceDemo
  6  {
  7     public static void main(String[] args)
  8     {
  9       int cansPerPack = 6;
 10       final double CAN_VOLUME = 0.355; // Liters in a 12-ounce can
 11       double totalVolume = cansPerPack * CAN_VOLUME;
 12  
 13       double pricePerPack = 2.59;
 14  
 15       double pricePerLiter = pricePerPack / totalVolume;
 16       
 17       System.out.print("Price per liter: ");
 18       System.out.println(pricePerLiter);
 19  
 20       // The integer part of the price gives the whole dollars.
 21  
 22       int dollars = (int) pricePerLiter;
 23  
 24       System.out.print("Dollars: ");
 25       System.out.println(dollars);
 26  
 27       // Multiply by 100 and round to the nearest integer to get the 
 28       // price in pennies.
 29  
 30       int pennies = (int) Math.round(pricePerLiter * 100);
 31       System.out.print("Pennies: ");
 32       System.out.println(pennies);
 33  
 34       // The cents are the last two digits of this value.
 35  
 36       int cents = pennies % 100;
 37       System.out.print("Cents: ");
 38       System.out.println(cents);
 39     }
 40  }