|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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); } |
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. ↩
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 |