|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
Give me signature for: if (blartz( "shoompah", 4.2 )) { ...} Give me *example calls* for a function that: - translates text from English to Spanish - computes somebody's taxes based (just) on their income; - computes somebody's taxes based (just) on their income & marital status; Now, can you give me a signature?
Write a sample call of: String wawa( int n, String wrd ) Consider the following function-signature: String representativeOf( int districtNum, String state ) Which of the following are reasonable guesses, as to what the function does? - given a representative's name and district, return their state? - given a districtNumer and state, return the name of the representative? - given a representative's name, return their district number and state? Call this function (giving it any legal inputs you like).
In hw-soln, we wanted:
“
If you had trouble going from the first phrasing (pseudo-code) to the second phrasing (actual code),
here's a way to help smooth that transition:
Example: Write a program for the following:
/** 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. * For example: * slicesReadyMsg( "mushroom", 3 ) * = "There are 3 piping hot pieces of mushroom pizza, ready to eat!" * slicesReadyMsg( __________, __ ) * = ______________________________________________ * slicesReadyMsg( __________, __ ) * = ______________________________________________ */ |
String slicesReadyMsg( String topping, int slicesReady ) { String slogan; // Declare; initialize in *both* branches of if-else. if (slicesReady == 1) { slogan = "There is " + slicesReady + " piping hot slice of " + topping + " pizza, ready to eat!"; } else { // slicesReady isn't equal to 1 slogan = "There are " + slicesReady + " piping hot slices of " + topping + " pizza, ready to eat!"; } return slogan; } |
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
/* (Same comments as before; omitted for lecture presentation) */ String slicesReadyMsg( String topping, int slicesReady ) { String verbForm; // The correct verb for our result. String plurality; // The noun-suffix, correctly plural or singular. if (slicesReady == 1) { verbForm = "is"; plurality = ""; } else { verbForm = "are"; plurality = "s"; } return "There " + verbForm + " " + slicesReady + + " piping hot " + ("slice" + plurality) + " of " + topping + " pizza, ready to eat!"; } |
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
Guideline: When using anif statement for initializing a variable, make sure each branch of theif initializes that variable.
(Even if you have an unreachable catch-all statement, you'll need to appease the compiler by initializing the variable there, too.)
Similarly, if you have a
This rule is less important; because it's clear that the first branch
without a
/** 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. * highwayDrivingTime( 0, true ) = 0 * highwayDrivingTime( 0, false ) = 0 * highwayDrivingTime( 65, true ) = 1.0 * highwayDrivingTime( 65, false ) = 1.6 * highwayDrivingTime( 130, true ) = 2.0 * 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 POTENTIAL_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 (isRoadClear) { return (miles / SPEED_LIMIT); } else { return (miles / SPEED_LIMIT) * POTENTIAL_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. * mustRegisterForDraft(22) == true * mustRegisterForDraft(14) == false */ boolean mustRegisterForDraft( int age ) { return (age >= 18); } |
if (age >= 18) { return true; } else { return false; } |
By the way, is this a silly function to write? Why, anybody who wants to call this function could more simply write the comparison against 18 directly. So what do we gain, by writing this as a separate function? We'll revisit this function soon.
A function which returns a
if (hasEatenSince( Fred, 2007, 01, 20 )) { return "I am stuffed!" } else { return "Is anybody else hungry?"; } |
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2012, Ian Barland, Radford University Last modified 2012.Sep.24 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |