|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Quickly recalling two of the expressions we typed in lab yesterday:
jo.pizzaArea(10) + jo.pizzaArea(2*6) jo.pizzaArea( Math.sqrt(64) ) |
When calling a function: “object dot methodName openParen argument”These two expressions further illustrate that the argument can be the result of a computation (2*6 or Math.sqrt(64)) and also that you can use the result of calling pizzaArea to do other stuff (like adding, in the first example).
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.
Write tuePizzaArea:
“20% Tuesdays”: order a pizza, and we'll bring you one with is 20% more area!solution (The code between /* and */ are comments, and not actual code. The exact format of the comments will be discussed in Friday's lab.)
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
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |