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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect02b
functions calling functions

We'll the lecture solidifying our knowledge, working through the examples of lab01b—your turn: a function from scratch: buffetPrice, toppingArea (missed for snow day). We'll stress:


Reading Assignment: Strings

Read the following section about Strings; we'll practice with them in lab tomorrow:

We have seen two types of data so far, in Java: int, and double. Today we consider a third: Strings.

Strings -- that is, strings of characters in a row -- are Java's name for what most people call text. For example,

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 ____________________________;
  }

Exercise:

/** Return something Beavis might say, in response to just 
 *  about any trigger word.
 * @param _____ The trigger word, for Beavis's response.
 * @return Beavis's catchphrase.
 *    p.beavis( "but" ) = "Heh heh, you said 'but'."
 *    p.beavis( "phenylalanine" ) = "Heh heh, you said 'phenylalanine'."
 *    p.beavis( "" ) = "Heh heh, you said ''."
 */
_____ beavis( _____ _____ ) {
  return ____________________________;
  }

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