We review the lab01 solutions, explaining why the laws of programming are good.
Then, role-play what happens when one function calls another (first for salesTaxOn(50), then for priceAfterTax(50), and then for crustArea(20)). Each function is its own person inside the computer; the only way to interact with the function is to pass it an argument (a number written down on a piece of paper), and get the answer returned (on another piece of paper).
In particular, the function has no idea of how you generated the number you passed to it (and it doesn't care). Conversely, you don't care how the function computes its answer; you just use the answer.
Pay close attention to the way crustArea(20) makes two separate calls to pieArea, once passing 20 (the value of diam) and once passing 16 (the value of diam - 4). The precise sequence should be understood, even though (confusingly?) pieArea happens to also call its parameter diam.
Discussion points:
[return-type] [function-name]( [type] [formal-parameter-name] ) { return [expression-involving-formal-parameter-name]; } |
(Replace items in brackets, like [type], with a particular type, like double or String. Items which are not in brackets, like return and ; must be typed literally.)
This isn't the complete syntax for Java functions. For example, later today we'll add a line of code just above the return statement.
final [type] [name] = [expression]; |
/** pieArea * Given a nonnegative diameter diam (in inches), return the surface * area of the pizza top (in square inches). * Accurate to within 1%. */ double pieArea( double diam ) { return Math.PI * (diam/2) * (diam/2); } |
Do we have repeated code?
A little bit, yes!
“diam/2” occurs twice,
and its purpose is the same: to convert
a pizza-diameter to a pizza-radius.
How to get rid of it?
We've actually already seen the tools we need: we can create a named constant:
/** pieArea * Given a nonnegative diameter diam (in inches), return the surface * area of the pizza top (in square inches). * Accurate to within 1%. */ double pieArea( double diam ) { double radius = diam/2; return Math.PI * radius * radius; } |
Actually, there are two local variables going on in pieArea. Can you see the other?
We have a picture:
variable | value |
---|---|
diam | |
radius |
Note: Well, we didn't declare radius as being final, although conceivably we could have.
Strings -- that is, strings of characters in a row -- are Java's name for what most people call text. For example, "Yo", "I am a String, with a capital S!" and "My length is 16.". Note that the quotation marks aren't part of the string itself; they just are Java's way of delimiting the start and end of a string constant. So the string "Yo" is only two characters long.
Strings are great for representing various data:
With numbers, Java has built-in arithmetic functions (as well as Math.sqrt, Math.cos, etc.), so you can start doing fun stuff with numbers immediately. What can we actually do with strings?
It turns out there are many interesting built-in functions on strings, but one which is particularly handy is concatenating strings. In part, it's handy because Java provides a shortcut name for this: +. That is,
"hello" + "there" |
"Hello" + " " + "there" + ", " + "sailor." |
...
...1
Look near the middle of the String:
"There once was a man from Purdue\nWhose limerick stopped at line two". |
(There are other escape sequences besides newline; the most important are ‘\"’ if you want to embed the quotation character inside a string, and ‘\\’ if you want to embed a backslash. You can look up more in the book under “escape sequences”.)
backIan's home page | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu | Last modified 2006.Aug.28 (Mon) |