RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect02a
functions calling functions; `this`
no repeated code!

Last time, we left off last time having talked about several approaches to writing fridayPizzaArea, where (on Friday's) every order is delivered with a bonus free-foot-Friday 12″ pizza.

comparing solutions

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;
  }
Version 2 is more readable, and it will be easier to spot any typing-errors that were made. In particular, the extra set of parentheses around the first part makes it clear that conceptually, we're computing two different numbers corresponding to the two pizzas.

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));
  }
Which is better, Version 3 or Version 2? Discuss.

Here is yet another variant:

double friPizzaArea_v4( double diam ) {
  return 0.785*(diam*diam + 144);
  }
Version four gives exactly the same answers as the other versions. Is it or less understandable/maintainable/clearly-correct that version 3?

This last version is the most different:

double friPizzaArea_v5( double diam ) {
  return pizzaArea(diam) + pizzaArea(12);
  }
Discuss Version 3 vs. Version 5: Version 5 is, hands down, far superior to Versions 1-4. While it's not as obvious to come up with that solution originally, once we've seen the trick, it's a clear winner.

Introducing this

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);
  }
In Java, this is a the name that a PizzaServer refers to themselves by. (It's a special name which is always present.) Thus, Version 6 is our final, best solution.1

Why re-use code?

The key feature to these programs is when we want to compute the area of (say) a 12″ pizza, we don't need to write 3.14*(12/2)*(12/2); we can write pizzaArea(12). What are the advantages of this approach?

This brings us to
The First Law of Programming: Do not repeat code, when you can re-use it.

Exercise

Krusteaze Ink.'s diskriminating kustomers sometimes throw keggers cocktail parties, and want to inkuire how much it will kost to buy (say) five large pizzas. Or, two large pizzas. Or, seventeen larges, or …. PizzaServers know that the price of a pizza is (fairly enough) proportional to its size: three cents per square inch (a bargain already; no volume discounts2). Fill in the blanks to complete the program below:

class PizzaServer {

   // ...other functions omitted...

  /**
   * @param n the number of large pizzas being ordered.
   * @return The price of n large (16") pizzas, in dollars.
   *    costOfLarges( 0) =    0
   *    costOfLarges( 1) =~   6.029
   *    costOfLarges( 2) =~  12.06
   *    costOfLarges(17) =~ 102.57
   */
  ______ costOfLarges( ______ n ) {
    _____  _______________;
    }

  }
(For reference, class PizzaServer is being projected onto the board; we'll alternate between displaying the interface and implementation views.)
solution (and discussion)


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.      

2Among their customers, Krusteaze Pizza is a commodity.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme