|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
We'll experiment with local variables, which were introduced in lecture yesterday.
Today's project will be done individually.
Group discussion:
int n; // The semicolon is part of the declaring-a-variable syntax). n = 34 * 56; // The semicolon is part of the initializing-a-variable syntax). n n+4 n+5 |
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); } } |
/** 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!"; } |
Note that there's something a bit fishy going on, in bookstoreAd.
It uses + with
a String on the left (
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 |
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.
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Jan.29 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |