|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Recall the two functions we've seen so far: ufoHeightAt, and pizzaArea. From these two examples, can we figure out the general syntax needed whenever we write a Java function?
signature { function-body } |
typereturn namefunction ( typeparam nameparam ) |
return expression; |
Note that when defining functions on your homework, you might prefer looking at the examples of other functions, or you might prefer the general-case syntax rules. It's a learning-style thing.
While we're talking about syntax, how about the syntax for calling a function? Quickly recalling the sorts of the expressions we typed in lab yesterday:
raki.ufoHeightAt(0) jo.pizzaArea(10) + jo.pizzaArea(2*6) jo.pizzaArea( Math.sqrt(64) ) |
object . namefunction ( expressionarg ) |
The above is official syntax; informally I find it's more helpful to memorize a couple of mantras:
To call a function, in Java: “object dot functionName openParen argument”
To declare a function, in Java: “returnType functionName openParen parameter-type-and-name”
Practice: Now it's your turn to write some functions.
Krusteaze Pizza is having a promotion:
Free foot Fridays: Order any pizza, and we'll throw in a 1-foot (12″) pizza for free!Write friPizzaArea — a function which takes in (as a parameter) the diameter which the customer orders, and returns the total area of pizza they'll get. Remember that we have a procedure for writing the function; it's not a procedure which relies on intuition:
jo.friPizzaArea(0) = jo.pizzaArea(12) = 113.04 |
teaching: When coming up with these test cases in class, don't have the previous test-cases for pizzaArea around; do actually type in the calls to jo.pizzaArea in the code pad.
For the following problems, it's important to write test cases first, to really understand what is being asked. (Also, read the offer closely, and follow it exactly!)
Write tuePizzaArea:
“20% Tuesdays”: order a pizza, and we'll bring you one with is 20% more area!solution
Finally, write wedPizzaArea:
Widening Wednesday — order any pizza, and they'll widen it by 10%!solution
The key feature to these programs is when we want to compute the area of (say) a 12″ pizza, we don't need to write 3.14*(12/2)*(12/2); we can write pizzaArea(12). What are the advantages of this approach?
Robust to changes:
Suppose Krusteaze decides, as a gimmick, to change their
pizzas from round to square, or even triangular.
Certainly this would entail changing pizzaArea.
But we wouldn't need to change friPizzaArea
(nor tuePizzaArea, wedPizzaArea below).
This is a huge win! In practice, programs aren't written once; they are written, and then modified, tinkered with, and overhauled for years. Writing them so that they are easy to re-write is critical.
More understandable: When other programmers look at the code and see “3.14*(12/2)*(12/2)”, they need to figure out what the heck that code is intended to do. But if they see “pizzaArea(12)”, it's clear right away, what the purpose of that statement is.
The First Law of Programming: Do not repeat code, when you can re-use it.
1 For any numeric function, 0 and 1 are obvious inputs. consider:
There once was a girl from Purdue Whose limerick stopped at line two.Or the more interesting test,
There once was a boy from VerdunAll good computer scientists also know there is a similar limerick about Nero, it goes without saying. ↩
2Can you spot the Pythagorean theorem at work, in jo.pizzaArea(12) + jo.pizzaArea(16) = jo.pizzaArea(20)? ↩
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.24 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |