Programming Exercises


graphics/pe_icon.gif
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 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)");     } } 
3.2

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.

3.3

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

 one three b d 

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



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net