3.1 | The following program will compile and run without errors: // Filename: Sunlight.java public class Sunlight { public static void main(String[] args) { // Distance from sun (150 million kilometers) /* The max value for int is 2147483647, so using int here will work. */ int kmFromSun = 150000000; // Again, using int for this value is OK. int lightSpeed = 299792458; // Meters per second // Convert distance to meters. /* The result of this equation will not fit in an int. Let's use a long instead. We need to ensure that the values that are multiplied really are multiplied using long data types, not multiplied as int data types and later converted to long. The L suffix on the 1000L integer literal ensures this. The value of kmFromSun will implicitly be converted from int to long to match the data type of the other factor. The conversion can be done implicitly by the compiler since the conversion represents a widening of the data type. */ long mFromSun = kmFromSun * 1000L; /* We know that the result value will fit in an int, but the compiler does not. We use an explicit cast to convince the compiler. The conversion must be specified explicitly, since the conversion represents a narrowing of the data type. */ int seconds = (int) (mFromSun / lightSpeed); System.out.print("Light will use "); printTime(seconds); System.out.println(" to travel from the sun to the earth."); } /* We leave this method alone. */ 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 | public class Binary { public static void main(String[] args) { System.out.println(makeBinaryString(42)); } public static String makeBinaryString(int i) { /* This section could have been optimized using StringBuffer, but is presented in this way for the sake of simplicity. */ String binary = ""; do { int lowBit = (i&1); String newDigit = ((lowBit == 0) ? "0" : "1"); binary = newDigit + binary; i >>>= 1; } while (i != 0); return binary; } } |
3.3 | The following program will operate as specified in the exercise. When given no arguments, the program will not print anything. public class ArgumentSkipper { public static void main(String[] args) { int count = args.length; // Iterate over the arguments skipping two places // forward between each step. for (int i=0; i < count; i+= 2) { System.out.println(args[i]); } } } |