-1

The program is about computing the temperature of a warm object after it has been put in a freezer.The freezer temperature is constant at -20 degrees. Once the object is in the freezer, it's temperature drops by (K * dt) degrees in each second, where K=0.001 and dt is the difference between the current object temperature and the freezer temperature. The task requires me to method should compute the time in seconds it takes for the object to cool down from the given initial temperature until it has reached a temperature that is less or equal to the target temperature.

The result should be an int value. The method should return the value -1 if the target temperature is less or equal to the freezer temperature. In the solution, I have to use a loop that keeps track of the changing temperature of the object second-by-second. I don't know how to implement the timeToCool method. Here is what I have so far:

public static int timeToCool(double initialTemperature, double targetTemperature) {
        return 0; 
    }

    public static void timeToCoolTest(double initialTemperature, double targetTemperature) {
        System.out.println("### Time To Cool");
        System.out.println("Initial temperature = " + initialTemperature);
        System.out.println("Target temperature = " + targetTemperature);
        int timeTaken = timeToCool(initialTemperature, targetTemperature);
        System.out.println("Time to cool = " + timeTaken + " seconds\n");
    }

Any help is appreciated thank you :).

Leonardo
  • 19
  • 1
  • 6
  • Curious assignment. It can be done with one method call, if you know calculus. – user207421 Mar 11 '16 at 23:52
  • 1
    This is a math question, it requires to solve a linear differential equation of the first order, which is an exponential function. But since one of your tags is "loops", it probably means your assignment is to try to solve it with numerical algorithm (it will converge very slowly). You can search for these terms. – Sci Prog Mar 11 '16 at 23:57

2 Answers2

1

You need to think of a different way to calculate the cooldown when reaching -20°, because towards the end the stepwise cooldown that happens gets infinitely small and will never reach -20°.

Similar behaviour as Zeno's paradox.

You can try it out with this code:

while (initialTemperature > targetTemperature)
    initialTemperature -= 0.001 * (initialTemperature - targetTemperature);
J. Schneider
  • 932
  • 1
  • 9
  • 19
  • The `dt` is an absolute value in degrees K per unit of time, so there is no paradox here – Germann Arlington Mar 11 '16 at 23:53
  • Of course there is, it is absolute in the sense of it always being positive clearly. But it will get infinitely small. E.g. when the difference is 1°, the temperature will only sink by 0.001°, and one step after that it'll sink by 0.999*0.001. You can try out my code, or write your own, then it'll probably become clearer. – J. Schneider Mar 12 '16 at 00:01
  • I see now that the naming of variable `dt` is confusing as it often references the time difference. Above it is defined as the temperature difference. – J. Schneider Mar 12 '16 at 00:09
0

This is one way to do what you're asking:

public static int timeToCool(double initial, double target) {
    if (initial < target) return -1;
    double K = 0.001;
    int time = 0;
    while (initial > target) {
        int dt = (initial - target);
        initial -=  K * dt;
        time++;
    }
    return time;
}
Leo Aso
  • 11,898
  • 3
  • 25
  • 46