Wednesday, January 29, 2014

Calculate the time it takes for light to travel from the sun to the earth?

Source:
import java.text.DecimalFormat;
 
public class Sunlight {
   public static void main(String[] args) {
 
      double distance = 92960000;
      double speed = 186282.397; 
 
      double time = Math.round(distance/speed);
 
      System.out.println("Total time: " + time + " seconds");
 
      double minutes = Math.floor(time / 60);
      double seconds = time % 60;
 
      DecimalFormat df = new DecimalFormat("#.##");
 
      System.out.println("It takes " + df.format(minutes) + " minutes and " 
            + df.format(seconds) + " seconds.");
 
   }
}

Output:
   # java Sunlight 
   Total time: 499.0 seconds
   It takes 8 minutes and 19 seconds.

Blog Archive