|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Today we'll experiment with Strings -- Strings represent text. This is a third type of data, in addition to int and double.
Start up a new BlueJ project, named something like “lab02b”. In the code pad (the lower-right corner of the main screen1), type in some sample strings, such as:
Exercise: together as a class, we'll 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 * * Examples, presuming that `mc` is an instance of Emcee: * mc.echo2( "hello" ) = "hello, hello." * mc.echo2( "Who's there?" ) = "Who's there?, Who's there?." * mc.echo2( "z" ) = ______ * mc.echo2( "" ) = ______ */ _____ echo2( _____ _____ ) { return ____________________________; } |
Preliminary: Make a new class Emcee; as before, take the file which BlueJ provides (with a few boilerplate definitions) and gut it out, replacing it with a single class definition, perhaps
class Emcee { // (We will add all our functions inside the class's curly-braces.) } |
/** Give a string suitable for introducing a certain name. * * @param name The name of the person being introduced. * @return a mundane introduction. * * Examples (if mcJo were an instance of class Emcee): * mcJo.introduce( "Snowball III" ) = "Hello, world; my name is Snowball III." * mcJo.introduce( "007" ) = "Hello, world; my name is 007." * mcJo.introduce( ____ ) = ______________________________ * mcJo.introduce( "" ) = "Hello, world; my name is ." */ // Put signature here. (See the above javadoc, for the parameter name.) |
reminder:Test introduce by creating an instance of class Emcee (by right-clicking on the tan class-definition-box). Name it, say, “mcJo” (or whatever you like). Then, call your functions in two different ways:Put String literals in quotes:
not
jo.echo2( "hello" )
jo.echo2( hello ) /* error -- cannot find variable hello */
Today we'll write a function which takes in two inputs. It's not a big deal, but we technically haven't mentioned it in Lecture. Here's an example of a function taking two inputs:
/** Return the cost of ordering several pizzas of a given size. * @param numPizzas THe number of pizzas ordered. * @param size The size (diameter) of the pizzas ordered, in inches. * @return The total price of the order, dollars. * Examples: * ps.costOfMany( 1, 16 ) =~ 6.03 * ps.costOfMany( 3, 16 ) =~ 18.09 * ps.costOfMany( 0, 16 ) = 0 * ps.costOfMany( 7, 0 ) = 0 */ double costOfMany( int numPizzas, double size ) { return numPizzas * this.pizzaArea(size) * 0.03; // 0.03 is the price per square inch of pizza, in dollars. // Note that the price may change, in the future. } |
typereturn namefunction( typeparam1 nameparam1, typeparam2 nameparam2 ) { return expression; } |
Agent 007 is known for his suave introduction2: "Bond... James Bond." Write the function introduceSpy, which creates such a greeting, given a first and last name:
/** Given a first and last name, figure out a dramatic greeting * (suitable for a spy). * * @param first The first name of the person being introduced. * @param last The last name of the peron being introduced. * @return A dramatic greeting (suitable for a spy) for the given name. * * Test cases: * * mcJo.introduceSpy( "Engelbert", "Humperdinck" ) = "Humperdinck... *Engelbert* Humperdinck." * mcJo.introduceSpy( "Dweezil", "Zappa" ) = ____________________________ * mcJo.introduceSpy( "McLovin", "" ) = ______________ * mcJo.introduceSpy( "", "Feist" ) = ______________ * mcJo.introduceSpy( "", "" ) = ______________ */ _______ introduceSpy( _____ ______, _____ _____ ) { return ________________________________________; } |
Before you start writing code, fill in the suggested test cases, with the desired output, character-for-character correct. Note how the test cases check the empty string. (When you have two inputs, you have more choices -- you can have one or the other or both inputs be degenerate.) Pay careful attention to spaces.
From the BlueJ project menu, choose Tools > Checkstyle. This will bring up an extra window with warning messages which can help detect errors sooner. For this class, code you turn in should not only compiles and passes the test cases, but also triggers no checkstyle warnings.
Now, write the body of the function. (Think: How did your test-cases go from the input to the output? What Strings did you need to concatenate (“+”)? Do the same thing, but in terms of your parameters instead of specific strings like “Dweezil” and “Zappa”.)
We will check this function off (including the test cases).
/** Given a Lectroid's last name (their first name is always "John"), * figure out a dramatic greeting (suitable for a spy). * * @param _____ The last name of the Lectroid. * @return A dramatic greeting (suitable for a spy) for the given Lectroid name. * * Test cases: * introduceLectroidSpy( "Parker" ) = "Parker... *John* Parker." * introduceLectroidSpy( "Z" ) = _________ */ // After test cases, write the signature here. |
Some people wonder, how Bond's standard introduction can be modified for people with three names — say, Lewis Scooter Libby.
There are two easy approaches:
Remember, don't repeat code!; your solution should not have (for example) an asterisk character anywhere, since that was already up in introduceSpy. This is a tougher problem, but by working through a couple of test cases by hand you should be able to solve it.
Some people don't like either of the two variants just mentioned -- because when giving just the (say) “Lewis Scooter” part of their name they could again make that yet more dramatic by saying “Scooter -- *Lewis* Scooter”, as a sub-part of their entire introduction.
Evaluate each of the two expressions, but first try to work out for yourself what the answers will be:
mcJo.introduceSpy( mcJo.introduceSpy( "Lewis", "Scooter" ), "Libby" ) // A left-associative approach mcJo.introduceSpy( "Lewis", mcJo.introduceSpy( "Scooter", "Libby" ) ) // A right-associative approach |
Once you understand what those examples are doing, decide which you like better3 Write a function introduceSpyBaroque3 which gives this ornate (convoluted!) introduction for a spy with three names.
1 If you don't see such a pane, select View > Show Code Pane. ↩
2 I don't suggest using this introduction at real-life cocktail parties; it only works in movies. ↩
3The right-associative version, or the left-associative? (resp.) ↩
4This difference isn't some left-vs.-right preference of the universe; it just stems from the fact that the "Bond-James-Bond" algorithm uses its right input twice, but the left input only once. ↩
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 |