O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required