Radford University ITEC

ITEC120

lect02:
Parameters, Local Variables, some scope, ...

Monday

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:

Tuesday

RE-CAP: Syntax.

local variables

Let's revisit pieArea:
  /** 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;
    }
Here, radius is a local variable. It is used as a temporary name; the name is only meanginful when somebody calls pieArea, and it goes away when pieArea returns its answer. Moreover, radius can only be used between the curly-braces which enclose the declaration.

Actually, there are two local variables going on in pieArea. Can you see the other?

We have a picture:

variable value
diam
radius
Whenever the function is working away, and it sees a name, it looks up the name in the table.

Note: Well, we didn't declare radius as being final, although conceivably we could have.

Strings

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:

(This which are not typically represented by strings include: prices or pizza sizes (use numbers (duh)); true and false (use booleans -- later), and individual characters like using 'M', and 'F' to encode gender (we'll talk about using individual characters soon).)

concatenating strings

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"
evaluates to "hellothere", and
"Hello" + " " + "there" + ", " + "sailor."
evaluates to "Hello there, sailor.".

...

...1


1

Look near the middle of the String:

"There once was a man from Purdue\nWhose limerick stopped at line two".
The two-character sequence "\n" actually represents one single character: a newline (or carriage return). We'll see later that output devices will display "Whose" on a new line from "Purdue". Nonetheless, when we're inside of Java programs, the newline (which is one single character) is written as a slash followed by an ‘n’.

(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”.)

     back


Ian's home page Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu Last modified 2006.Aug.28 (Mon)