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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab03a
Bookstore Ads
local variables; automatic conversion to Strings

We'll experiment with local variables, which were introduced in lecture yesterday.

Today's project will be done individually.

Group discussion:

Today's tasks

  1. Convert the following function to use two local variables: amountOrdered, and fridayBonusAmount. (If you like you can also attach a name to the diameter of the friday bonus pizza.)
    class PizzaServer {
      // other methods omitted...
    
      /** Calculate the area of pizza ordered, given its diameter,
       * based on the "free foot friday" promo of itec120-lect01c.
       * @param diam The diameter of the pizza ordered, in inches.
       * @return The total area of pizza received, in square inches.
       *
       * Examples, if p is an instance of PizzaServer:
       *  p.friPizzaArea( 0) = p.pizzaArea( 0) + pizzaArea(12) =~ 113.04
       *  p.friPizzaArea( 2) = p.pizzaArea( 2) + pizzaArea(12) =~ 116.18
       *  p.friPizzaArea(12) = p.pizzaArea(12) + pizzaArea(12) =~ 226.08
       *  p.friPizzaArea(20) = p.pizzaArea(20) + pizzaArea(12) =~ 427.04
       */
      double friPizzaArea( double diam ) {
        return this.pizzaArea(diam) + this.pizzaArea(12);
        }
    
      }
    
    Solutions will be available this afternoon.
  2. Convert the following function to use no local variable, just one return statement.
      /** Create an advertising slogan for our bookstore,
       * involving books whose name is words followed by a number,
       * like "Fahrenheit 451".
       * 
       * @param titleStart The first word(s) of the book.
       * @param num The number which completes the title.
       * @return An advertising slogan to make various posters with.
       * Examples:
       *   p.bookstoreAd( "Slaughterhouse", 5 )  // Vonnegut
       *     = "Books-A-Jillion: we stock 6 copies of Slaughterhouse 5, guaranteed!"
       *   p.bookstoreAd( "", 1984 )             // Orwell
       *     = "Books-A-Jillion: we stock 1985 copies of  1984, guaranteed!"
       *       (Hmm, an extra space there, in the book-title...)
       *
       *    Other possible book titles: Catch-22 (Heller), Mila 18 (Uris), 
       *      Fahrenheit 451 (Bradbury), 21 (Iverson), Population: 485 (Perry).
       */
      String bookstoreAd( String titleStart, int titleNum ) {
      
        String fullTitle;  // The full title of the book.
        fullTitle = titleStart + " " + titleNum;
      
        int numToStock;        // How many copies is enough to stock?
        numToStock = (titleNum + 1);
      
        String storeName;  // The name of the bookstore.
        storeName = "Books-A-Jillion"
      
        return storeName + ": we stock " + numToStock 
               + " copies of " + fullTitle + ", guaranteed!";
        }
    
    Solutions will be available this afternoon.
  3. Discuss with your neighbor, when they've also finished: Which versions do you prefer best?
  4. Discuss with your neighbor: Do the javadoc comments need to be updated?
  5. Note that there's something a bit fishy going on, in bookstoreAd. It uses + with a String on the left ((titleStart + " ")), and a number (titleNum) on the right! I claimed earlier that + either adds numbers, or it concatentates Strings, but that it can't handle a number plus a String.

    That's partly true, and partly lie. The truth is, it doesn't make sense to combine Strings and numbers. But a number can always be converted to a String, and so (behind your back) Java re-writes the above code so that instead of numTitle, it uses Integer.toString( numTitle ) — we'll discuss the funny-looking function Integer.toString later; suffice it to say it's a function which takes in an int, and returns a String representation of that number (in base 10). That way, by the time + looks around, it sees a String on both sides of it, so string-concatentation is used.

    While it's nice of Java to do this conversion for us, sometimes it leads to rather surprising results, if you're not careful. Evaluate the following, and discuss the results with your neighbor:

     1 + (2  + " blind mice")
    (1 +  2) + " blind mice"
     1 +  2  + " blind mice"
    ("blind mice " +  2) + 1
     "blind mice " + (2  + 1)
     "blind mice " +  2  + 1
    
    When you don't provide parentheses, Java fills them in for you. What do you think Java's rule is, for how it fills in parentheses? (We say that Java's + is “left-associative”. In Java, most operators are left-associative but number of them are right-associative.)
    Edict: If it makes things clearer, include parentheses, even if they aren't strictly needed. (Even if you you have memorized the rather unwieldy table of Java's association and precedence rules, other people reading your code might not have.)

We will check you off on the above two functions (friPizzaArea with local variables, and bookstoreAd without any local variables).

Note: In general for the semester, we'll check off work on some labs (and enter that in the gradebook), and we might not for others. Overall, more than half the labs will be checked off, and lab material is (of course) always be fair game for homework and test questions.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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