|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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 ) { _____ _______________; } } |
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:
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:
"" |
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" |
"Hello" + " " + "there" + "," + " sailor." |
tip: It's easy to forget the + between strings. Spot the missing plus in:Also, it's easy to get 0 or 2 spaces, instead of 1, when doing a lot of string concatenation.
"Hello " + "there" + ", " "Sailor" + " " + "Moon"
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. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |