RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect02a
local variables and names constants

Review/warm-up

Review: The Laws of Programming (so far)

Warm-up problem: 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, nineteen 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 discounts1). 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)

Strings

Strings -- that is, strings of characters in a row -- are Java's name for what most people call text. For example, "Yo", "I am a String, with a capital S!" and "My length is 16.". Note that the quotation marks aren't part of the string itself; they just are Java's way of delimiting the start and end of a string constant. So the string "Yo" is only two characters long.

Strings are great for representing various data:

(Thing which are not best represented by strings include: prices or pizza sizes (use numbers (duh)); true and false (use booleans — later), and individual characters like using 'M' and 'F' to encode gender (we'll talk about using individual characters soon).)

The empty string

We saw a String with five characters between the quotation marks, and another with two character. Can you write down a String with only one character? Okay, good. How about even shorter than one character? Sure! Type a quotation-mark, then type zero characters, and then the closing quotation-mark:

""
This String is called the empty string. Just as 0 is a particularly simple number to do arithmetic with, we'll see that the empty string is particularly easy to do string-arithmetic with.

concatenating strings

With numbers, Java has built-in arithmetic functions like + and * (in addition to the special math-package functions Math.sqrt, Math.cos, etc.), so you can start doing fun stuff with numbers immediately. What can we actually do with strings?

It turns out there are many interesting built-in functions on strings, but one which is particularly handy is concatenating strings. In part, it's handy because Java provides a shortcut name for this: +. That is,

"hello" + "there"
evaluates to "hellothere", and
"Hello" + " " + "there" + "," + " sailor."
evaluates to "Hello there, sailor.".

tip: It's easy to forget the + between strings. Spot the missing plus in:
"Hello " + "there" + ", " "Sailor" + " " + "Moon"
Also, it's easy to get 0 or 2 spaces, instead of 1, when doing a lot of string concatenation.

Just as 0 and 1 are numbers that are particular easy to compute with, the empty string, "", is particularly easy to concatentate. Always use "" as a test case, unless that's a clearly nonsensical test case.

Exercise: Complete the following function, using + for Strings:

/** Return the sound of an echo, of a given phrase.
 * @param _____ The phrase to shout at a cliff-side.
 * @return The sound returned
 *    p.echo2( "hello" ) = "hello, hello."
 *    p.echo2( "Who's there?" ) = "Who's there?, Who's there?."
 *    p.echo2( "z" ) = ______
 *    p.echo2( "" )  = ______
 */
_____ echo2( _____ _____ ) {
  return ____________________________;
  }


1Among 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