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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect01c-friPizzaArea-comparisons
comparing three approaches

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.

Introducting 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


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.      

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