|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Last time, we left off last time having talked about several approaches to writing fridayPizzaArea, where (on Friday's) every order is delivered with a bonus free-foot-Friday 12″ pizza.
We consider several different possible solutions to writing friPizzaArea. They all give the same answers, but some are better than others.
We see from our test cases, that the answer we want is the same as pizzaArea, with an extra 113 square inches. That naturally suggests:
double friPizzaArea_v1( double diam ) { return 3.14*(diam/2)*(diam/2)+113.04; } |
Here is a barely-different version -- only some added spaces and parentheses -- yet it is decidably a bit better:
double friPizzaArea_v2( double diam ) { return (3.14*(diam/2)*(diam/2)) + 113.04; } |
A related way of writing this would be:
double friPizzaArea_v3( double diam ) { return (3.14*(diam/2)*(diam/2)) + (3.14*(12.0/2)*(12.0/2)); } |
Here is yet another variant:
double friPizzaArea_v4( double diam ) { return 0.785*(diam*diam + 144); } |
This last version is the most different:
double friPizzaArea_v5( double diam ) { return pizzaArea(diam) + pizzaArea(12); } |
Alas, there is one problem Version 5. Do you remember, how we call a function, in Java? Our mantra:
To call a function: object dot functionName openParen argument. And as written, Version 5 doesn't have an “object dot”! This raises an odd question: when somebody asked jo the PizzaServer to compute a friPizzaArea, she needs to find out some regular pizzaAreas. She looks around for another PizzaServer for her to ask those sub-questions of, but none are in sight. So who does she ask? Herself! Here is the complete code:
double friPizzaArea_v6( double diam ) { return this.pizzaArea(diam) + this.pizzaArea(12); } |
definition: this: a name (variable) which always refers to the object currently being asked a question.It only make sense to use this inside a function-body (not the code pad), since otherwise we're not currently in the process of answering a question! Thus, Version 6 is our final, best solution.1
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.
Krusteaze Ink.'s diskriminating kustomers sometimes throw
keggers cocktail parties,
and want to inkuire how much it will kost to buy (say) five large pizzas.
Or, two large pizzas. Or, seventeen larges, or ….
PizzaServers know that the price of a pizza is (fairly enough)
proportional to its size: three cents per square inch (a bargain already;
no volume discounts2).
Fill in the blanks to complete the program below:
class PizzaServer { // ...other functions omitted... /** * @param n the number of large pizzas being ordered. * @return The price of n large (16") pizzas, in dollars. * costOfLarges( 0) = 0 * costOfLarges( 1) =~ 6.029 * costOfLarges( 2) =~ 12.06 * costOfLarges(17) =~ 102.57 */ ______ costOfLarges( ______ n ) { _____ _______________; } } |
1 Actually, if you leave off this, then Java re-write your program behind your back, to include the word. Most people leave it off, but don't be fooled: it really is there, and the Java syntax for calling a function really is object-dot-functionName-openParen-argument. ↩
2Among their customers, Krusteaze Pizza is a commodity. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Feb.27 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |