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

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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote