RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect03a
role-playing function calls; scope

parameters, locals, and their scope

We'll get three volunteers, and will role-play what happens when one function calls another. 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.

We revisit our solution to last week's lab, where we wrote toppingArea and crustArea. We've updated it a bit to add a couple of local variables (for practice).

/** Functions to calculate the area of pizza purchased, given a diameter.
 *
 * @author Ian Barland
 * @version 2007.Jan.10
 */
class PizzaServer {

  /** Calculate the area of a pizza, given its diameter.
   * @param diam The diameter of the pizza, in inches.
   * @return The area of the pizza, in square inches.
   *    (test cases omitted, to fit on one screen in class)
   */
  double pizzaArea( double diam ) {
    double radius;
    radius = diam/2;
    return 3.14 * radius * radius;
    }


  /** Calculate the are of *topping* on a pizza, given its diameter.
   *  Krusteaze pizza has 3" of crust all the way around.
   * @param diam The diameter of the pizza (in inches); must be 6 or more.
   * @return The amount of topping (in square inches)
   *    (test cases omitted, to fit on one screen in class)
   */
  double toppingArea( double diam ) {
    double toppingDiam;
    toppingDiam = diam - 2*3;
    return this.pizzaArea( toppingDiam );
    /* We subtract the part of the diameter which is crust: 3" on each side. */
    }


  /** Calculate the are of *crust* on a pizza, given its diameter.
   *  Krusteaze pizza has 3" of crust all the way around.
   * @param diam The diameter of the pizza (in inches); must be 6 or more.
   * @return The amount of crust (in square inches)
   *    (test cases omitted, to fit on one screen in class)
   */
  double crustArea( double diam ) {
    return this.pizzaArea(diam) - this.toppingArea(diam);
    }

  }

Practice We'll get three volunteers; if you want to play the role of toppingArea or pizzaArea it helps to have a calculator.
Q: I call crustArea, handing them a sheet of paper with “20” written on it. What happens?

Teacher: You'll want three bold markers, some sheets of blank paper passing and returning results, and these placards (.doc). (The two copies of pizzaArea placard is intentional: it's called twice, with a different input each time.)

Pay close attention to the way pizzaArea is called two separate time -- once with a value of 20, and once with the value of 14.

Discussion points:

Review: syntax-week2—Java Syntax: week 2

Applied Example (inside, say, class Emcee):

  /** How many characters are needed, to print somebody's name?
   * @param fname The person's first name.
   * @param lname The person's last  name.
   * @return the number of characters needed to print the full name.
   *  Examples, if djMo were an Emcee:
   *    djMo.charsNeeded( "Jane", "Doe" ) =                 
   *    djMo.charsNeeded( "Jane", ""    ) =                 
   *    djMo.charsNeeded(     "", ""    ) =                 
   */
  int charsNeeded( String fname, String lname ) {
    return                 ;
    
    }
(soln)
You'll do something similar on the homework -- have a method which is given some string, and to do its job it needs to call another method: not another Emcee method, but a String method! Thus in our function call “object dot methodName arguments”, the object will be one of the Strings we were handed!

Note that we don't yet know how to write correct code for the above function. (How do we sometimes add 1 for the space between the two names, and sometimes not?) We'll be better able to do this in a couple of days, after talking about booleans and if-else.


1This mode of thinking is not a great mode of reasoning about issues with many subtle and intertwined causes, like gender inequities, divorces or other wars.)      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Jan.24 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme