Some drinks are sold in four-packs instead of six-packs. How would you change th
ID: 3789214 • Letter: S
Question
Some drinks are sold in four-packs instead of six-packs. How would you change the following program to compute the total volume for four-packs instead of six-packs?
/*
CanVolCalc ; number, number
program calculates and outputs, in liters:
1. the volume of a six-pack of 12-ounce cans
2. the volume of a six-pack of 12-ounce cans and a 2-liter bottle
program assumes the volume of a can is 12 ounces (0.355 liter)
*/
public class CanVolCalc
{
public static void main(String[] args)
{
int cansPerPack = 6;
final double CAN_VOLUME = 0.355; // Liters in a 12-ounce can
double totalVolume = cansPerPack * CAN_VOLUME;
System.out.print("A six-pack of 12-ounce cans contains ");
System.out.print(totalVolume);
System.out.println(" liters.");
final double BOTTLE_VOLUME = 2; // Two-liter bottle
totalVolume = totalVolume + BOTTLE_VOLUME;
System.out.print("A six-pack and a two-liter bottle contain ");
System.out.print(totalVolume);
System.out.println(" liters.");
}
}
Explanation / Answer
public class CanVolCalc {
public static void main(String[] args) {
int cansPerPack = 6;
final double CAN_VOLUME = 0.355; // Liters in a 12-ounce can
double totalVolume = cansPerPack * CAN_VOLUME;
System.out.println("------------------------------------------");
System.out.println("**********Calculation for six-packs***********");
System.out.print("A six-pack of 12-ounce cans contains ");
System.out.print(totalVolume);
System.out.println(" liters.");
final double BOTTLE_VOLUME = 2; // Two-liter bottle
totalVolume = totalVolume + BOTTLE_VOLUME;
System.out.print("A six-pack and a two-liter bottle contain ");
System.out.print(totalVolume);
System.out.println(" liters.");
/**
* for four pack method call
*/
System.out.println("------------------------------------------");
System.out.println("**********Calculation for four-packs***********");
canVolCalfor4Pack(4,2);//first parameter is cans Per Pack and 2nd parameter is bottle volume in ltr
}
private static void canVolCalfor4Pack(int cansPerPack, double BOTTLE_VOLUME) {
// TODO Auto-generated method stub
final double CAN_VOLUME = 0.355; // Liters in a 12-ounce can
double totalVolume = cansPerPack * CAN_VOLUME;
System.out.print("A four-pack of 12-ounce cans contains ");
System.out.print(totalVolume);
System.out.println(" liters.");
totalVolume = totalVolume + BOTTLE_VOLUME;
System.out.print("A four-pack and a two-liter bottle contain ");
System.out.print(totalVolume);
System.out.println(" liters.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.