|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Today we will work through several examples as a group.
/** 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 slicesReady The number of currently ready-to-serve * slices with the given topping. * @return A complete sentence describing the inventory. * For example: * jo.slicesReadyMsg( "mushroom", 3 ) * = "There are 3 piping hot slices of mushroom pizza, ready to eat!" * jo.slicesReadyMsg( "pepperoni", 0 ) * = "There are 0 piping hot slices of pepperoni pizza, ready to eat!" * jo.slicesReadyMsg( "mushroom", 1 ) * = "There is 1 piping hot slice of mushroom pizza, ready to eat!" */ String slicesReadyMsg( String topping, int slicesReady ) { if (slicesReady == 1) { return "There is " + slicesReady + " piping hot slice of " + topping + " pizza, ready to eat!"; } else { // slicesReady isn't equal to 1 return "There are " + slicesReady + " piping hot slices of " + topping + " pizza, ready to eat!"; } } |
This function works, but ech, it's full of repeated code. In particular -- if you later realize that (say) you forgot a space somewhere in the message, you might fix the code in place but forget to update the other copy. Or, if your boss decides that the message should read something like “Only 5 pizzas of pepperoni left -- get 'em while you can!” then you again need to remember to update multiple places in the code.
We can do better: Where do the two solutions differ? In only two places: “is” vs. “are”, and the suffix “s” vs. no suffix at all. We can factor out the common code, and let our if statement focus on just those differences:
/* (Same comments as before; omitted for lecture presentation) */ String slicesReadyMsg( String topping, int slicesReady ) { String verbForm; // The correct verb for our result. String pluralSuffix; // The noun-suffix, correctly plural or singular. // We just declared two local variables; now initialize them: if (slicesReady == 1) { verbForm = _____; pluralSuffix = _____; } else { _________ = _____; ____________ = _____; } return ______________; } |
We used to have a rule that immediately after declaring a variable, you should initialize it. We still have that rule! Sometimes it's an if-else statement which is doing the initialization, but that's fine. Just be careful:
Guideline:
- When using an if-else statement for initializing a variable, make sure both branches of the if-else statment initialize that variable.
- Likewise, when using an if statement to return an answer, make sure both branches of the if-else statement returns an answer.
Similarly, if you have a return inside a
branch of an if statement,
consider writing your code so that every
branch of the if statement has a return.
This rule is less important; because it's clear that the first branch
without a return essentially starts an else
block which lasts for the rest of the function.
/** Give an estimate of how long 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. * jo.highwayDrivingTime( 0, true ) = 0 * jo.highwayDrivingTime( 0, false ) = 0 * jo.highwayDrivingTime( 65, true ) = 1.0 * jo.highwayDrivingTime( 65, false ) = 1.6 * jo.highwayDrivingTime( 130, true ) = 2.0 * jo.highwayDrivingTime( 130, false ) = 3.2 */ double highwayDrivingTime( double miles, boolean isRoadClear ) { int SPEED_LIMIT = 65; // posted limit, in MPH. (We can declare and initialize, all on one line.) double DELAY_FACTOR = 1.60; // 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). if (______________) { return (miles / SPEED_LIMIT); } else { return (miles / SPEED_LIMIT) * DELAY_FACTOR; } } |
We can also return a true/false answer from a function:
/** * @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. * jo.mustRegisterForDraft(22) == true * jo.mustRegisterForDraft(14) == false */ |
A function which returns a boolean value is called a predicate function (or just, “predicate”). Often, predicates' names start with “is” or “has”: For example “isHappy”, “hasEaten”, etc.. So if these names happened to be methods for PizzaServer, our code might naturally have lines like
if (jo.hasEatenSince( 2007, 09, 01 )) { return "I am stuffed!" } else { return "Is anybody else hungry?"; } |
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Jan.31 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |