Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that
ID: 671684 • Letter: S
Question
Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. You want to know ahead of time how many completely filled jars you will have. The following program has been written for that purpose. What is wrong with it? Why? How can you fix it?
public class MilkJarCalculator
{
public static void main(String[] args)
{
double milk = 5.5; // gallons
double jarCapacity = 0.75; // gallons
int completelyFilledJars = milk / jarCapacity;
System.out.println(completelyFilledJars);
}
}
Explanation / Answer
The problem with this approach is if the value returned by dividing milk/jarCapacity is 5.8 for example than the value assigned to the completelyFilledJars will be 6 but here only 5 jars will be completely filled and one jar will be 80% filled.For finding out the number of completely filled jars use Math.floor(milk/jarCapacity)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.