Coffee.java


Below is the syntax highlighted version of Coffee.java from §1.3 Conditionals and Loops.


/*************************************************************************
 *  Compilation:  javac Coffee.java
 *  Execution:    java Coffee r
 *  
 *  Simulate the cooling of coffee accoring to Newton's law of cooling
 *  with Euler's algorithm for numerically solving a differential
 *  equation.
 *
 *
 *************************************************************************/

public class Coffee {

   public static void main(String[] args) {
      double r = Double.parseDouble(args[0]);    // cooling constant
      double room   = 17.0;                      // room temperature (Celsius)
      double coffee = 82.3;                      // coffee temperature (Celsius)
      double goal   = 39.5;                      // goal temperature (Celsius)
      double dt     = 0.001;                      // time quantum
      double time   = 0.0;                       // current time

      while (coffee > goal) {
         coffee += -r * (coffee - room) * dt;
         time += dt;
         System.out.println(time + ": " + coffee);
      }
   }
}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.