Programming Exercises



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:
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 ");
        System.out.println(" to travel from the sun to the earth.");

    public static void printTime(int sec) {
        int min = sec / 60;
        sec = sec - (min*60);
        System.out.print(min + " minute(s) and " + sec + " second(s)");

Create two method that both takes an int as an argument and returns a String object representing the binary representation of the integer. Given the argument 42, it should return "101010". The first method should calculate the binary representation manually, and the other should use the functionality available in the Java class libraries.


Create a program that will print every other argument given on the command line. If the program was executed with the following on the command line,

java ArgumentSkipper one two three a b c d

the program would print


Consider how your program would operate when no arguments are given.