|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Alas, just getting a function to run isn't enough. Why not? Well, consider coming across the following code (which does run):
double f( double r ) { return 3.14 * r * r * r / 3; } |
The Zeroth Law of ProgrammingAt the least, this law requires that each function must have a clear description of what it computes. Even if a computer might be able to do something with undocumented code, it's useless to people (including the person who wrote it, after a week, when they don't remember exactly what each function does.)
Code is meant to be read by humans.
(For example: One very easy mistake to make with functions we've written, is remembering that pizzaArea should be passed the pizza's diameter, not the radius. There is no way for the computer to help us remember this.)
We'll come back later to what comprises an adequate description. Note that just the fact of choosing a good “self-documenting” name goes a long way; avoid generic names like f or calculate. But, just because a name seems self-documenting to you doesn't necessarily mean that further comments aren't needed. For example,
double rightConeSurfaceArea( double r ) |
Okay, back to our pizzaArea example, Attempt 3, where we add a description. Descriptions precede the function, and are enclosed by /* and */.
/** Calculate the area of a pizza, given its diameter. * @param diam The diameter (in inches); must be non-negative. * @return the surface area of the pizza top (in square inches). * Accurate to within 1%. * pizzaArea(16) ~= 200.96 * pizzaArea(12) ~= 113.04 * pizzaArea(20) ~= 314.0 * pizzaArea( 0) ~= 0.0 * pizzaArea( 2) ~= 3.14 */ double pizzaArea( double diam ) { return 3.14 * (diam/2) * (diam/2); // Single-line comments can also start with '//', // and they last until the end of that line. } /* You can have comments which begin with '/*', * instead of '/**'. These are just regular multi-line comments, * and are not used to generate java documentation. */ /* By the way, you don't need that row of stars down the left side; that's just to improve readability. */ |
There is a bonus to writing this information using those tags @param and @return: In BlueJ, look at the code window, and in the upper-right corner change Implementation to instead ask for the Interface view. Voila! Other programmers can look at this web page for pizzaServer, and know how to call our functions, without ever actually having to look at our code!
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
For today's lab, you must work with a partner. We will check off your work, before leaving.
In yesterday's lecture, we saw that one function (like friPizzaArea) might call another function (like pizzaArea).
Some customers are concerned with the surface area of their pizza. Others ignore the crusts, and want to ask the server how much topping-area each pizza comes with. The server happens to know that Krusteaze Pizzas have a 3″ border of crust all the way around them (what cheapskates!).
Write the function toppingArea.
One last problem (not needed for check-off, but you should
be able to work through it):
Some people actually like the crust of a pizza!
They want to ask PizzaServers how
much crust there is, on a pizza of a given size!
Follow the same steps above, to write crustArea. (In my solution, the only arithmetic inside crustArea is a single subtraction.)
1 the area of a 2-d triangle is 1/2 base times height; the area of a 3-d cone is 1/3 base times height. ↩
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 |