August 2003
Intermediate to advanced
672 pages
16h 23m
English
Programming Exercises |
| 3.1 |
The program below is supposed to calculate and show the time it takes for light to travel from the sun to the earth. It contains some logical errors. Fix the program so that it will compile and show the intended value when run.
// Filename: Sunlight.java public class Sunlight { public static void main(String[] args) { // Distance from sun (150 million kilometers) int kmFromSun = 150000000; int lightSpeed = 299792458; // meters per second // Convert distance to meters. int mFromSun = kmFromSun * 1000; int seconds = mFromSun / lightSpeed; System.out.print("Light will use "); printTime(seconds); System.out.println(" to travel ... |
Read now
Unlock full access