/** Examples of functions using booleans * (has helper values, as inputs, as outputs). * See * http://www.radford.edu/~itec120/2010fall-ibarland/Lectures/lect03b.html * @author Ian Barland * @version 2010.Sep.15 */ class Lect03b { static double myMax( double x, double y ) { if ( x >= y ) { return x; } else { return y; } } /** Test our boolean functions */ static void testBooleans() { System.out.println( "Max of 10, 2:" ); System.out.println( "Actual: " + myMax( 10,2) ); System.out.println( "Expected: 10" ); System.out.println( "Max of 2, 10:" ); System.out.println( "Actual: " + myMax( 2,10 ) ); System.out.println( "Expected: 10" ); System.out.println( "Max of 3, 3:" ); System.out.println( "Actual: " + myMax( 3,3 ) ); System.out.println( "Expected: 3" ); System.out.println( "Inventory message for 3 mushrooms: " ); System.out.println( "Actual: \""+ slicesReadyMsg( "mushroom", 3 ) + "\"" ); System.out.println( "Expect: \"There are 3 piping hot pieces of mushroom pizza, ready to eat!\""); System.out.println(); System.out.println( "Inventory message for 0 pepperoni: " ); System.out.println( "Actual: \""+ slicesReadyMsg( "pepperoni", 0 ) + "\"" ); System.out.println( "Expect: \"There are 0 piping hot pieces of pepperoni pizza, ready to eat!\""); System.out.println(); System.out.println( "Inventory message for 1 pepperoni: " ); System.out.println( "Actual: \""+ slicesReadyMsg( "pepperoni", 1 ) + "\"" ); System.out.println( "Expect: \"There is 1 piping hot piece of pepperoni pizza, ready to eat!\""); System.out.println(); System.out.println( "driving time for 0m when road clear." ); System.out.println( "Actual: " + highwayDrivingTime(0,true) ); System.out.println( "Expect: 0" ); System.out.println(); System.out.println( "Must a 0-year-old register for the draft?" ); System.out.println( "Actual: " + mustRegisterForDraft(0) ); System.out.println( "Expect: false" ); System.out.println(); System.out.println( "Actual: " + qualifies100m( 0, 'M' ) ); System.out.println( "Expect: true" ); System.out.println( "Actual: " + qualifies100m( 10.5, 'F' ) ); System.out.println( "Expect: true" ); System.out.println( "Actual: " + qualifies100m( 13.6, 'M' ) ); System.out.println( "Expect: false" ); System.out.println( "Actual: " + qualifies100m( 13.6, 'F' ) ); System.out.println( "Expect: false" ); System.out.println( "Actual: " + qualifies100m( 10.07, 'M' ) ); System.out.println( "Expect: true" ); System.out.println( "Actual: " + qualifies100m( 11.13, 'F' ) ); System.out.println( "Expect: true" ); System.out.println( "Actual: " + qualifies100m( 11.13, 'M' ) ); System.out.println( "Expect: false" ); System.out.println( "========" ); } /** Given how many slices of (say) mushroom pizza are * currently available, create a nice tempting message * to advertise (shout out, or post, or put on a * LED sign...) * * @param topping The type of pizza (e.g. "pepperoni") * @param piecesReady The number of currently ready-to-serve * slices with the given topping. * @return A complete sentence describing the inventory. */ static String slicesReadyMsg( String topping, int slicesReady ) { String verbForm; String plural; if (slicesReady != 1) { verbForm = "are"; plural = "s"; } else { i verbForm = "is"; plural = ""; } return "There " + verbForm + " " + slicesReady + " piping hot piece" + plural + " of " + topping + " pizza, ready to eat!"; } /** Give an estimate of how far it will take to travel a given distance, on the highway. * @param miles How far to travel, in miles. * @param isRoadClear Is the road in good condition (true), or is it icy/foggy/accidented (false)? * @return The estimated time of driving that far, in hours. */ static double highwayDrivingTime( double miles, boolean isRoadClear ) { int SPEED_LIMIT; // posted limit, in MPH. SPEED_LIMIT = 65; double DELAY_FACTOR; // If road isn't clear, how much does that slow us down? // 2.0 means twice as long, 1.0 is no slowdown (and less than 1.0 would be a speedup). DELAY_FACTOR = 1.6; return (miles / SPEED_LIMIT); } /** * @param age The age (in years) of the person in question. E.g., 18months is age 1. * @return whether or not the person must register with Selective Service. * mustRegisterForDraft(22) == true * mustRegisterForDraft(14) == false */ static boolean mustRegisterForDraft( int age ) { return true; } static boolean qualifies100m( double time, char gender ) { if (gender=='F') { return (time <= 11.13); } else { if (time <= 10.07) { return true; } else { return false; } } } }