/** * Demonstrates random numbers, integer arithmetic, floating point comparison * @author Pete Nordquist * @version 100203 */ public class W05N02 { // use the green and white icon to call this method public static void roundIt(double d) { // call Math.round() long rslt = Math.round(d); System.out.println("rounded number is " + rslt); // create a 'random' number double d1 = Math.random(); // round this number System.out.println("d is " + d1 + ", rounded number is " + Math.round(d1)); } public static void intPromtion(int i) { // promotion by assignment // the assignment statement makes i into a double double d = i; // promotion by arithmentic operation System.out.println(5.0/i); // compare above with System.out.println(5/i); } // try the example shown in Week 5, Notes 2 // call intDiv and supply the values 7, 5 public static void intDiv(int numerator, int denominator) { System.out.println("numerator / denominator == " + numerator / denominator); System.out.println("numerator % denominator == " + numerator % denominator); } // Using a tolerance value to compare doubles or floats // try the example in Week 5, Notes 2 public static boolean compareDoubles(double d1, double d2, double tolerance) { //System.out.println("d1 / d2 == " + (d1 / d2)); if (Math.abs(d1 - d2) < tolerance) return true; else return false; } }