|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
We'll experiment with Strings, which were introduced in lecture yesterday.
Today's project will be done in pairs.
If you didn't finish last week's lab, raise your hand at the start of this lab, and a PI will look at it.
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. (Although you did use it on hw00!) 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 orderd, 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; } |
In lecture, we saw functions dealing with Strings. When you call them in BlueJ, be sure to include quotation marks around the letters you want to string together:
jo.echo2( "hello" ) |
jo.echo2( hello ) /* error -- cannot find variable hello */ |
Preliminary:
class Emcee { } |
Go to BlueJ's code pad in the lower-right corner of the main screen1. Type in some sample strings, such as:
As just discussed in the lecture yesterday, +, if applied to Strings, will concatenate them. Try these in the Code Pane:
/** 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( "" ) = "Hello, world; my name is ." */ |
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:
/* Test cases: * mcJo.introduceSpy( "Ian", "Barland" ) = "Barland... *Ian* Barland." * mcJo.introduceSpy( "Dweezil", "Zappa" ) = "Zappa... *Dweezil* Zappa." * mcJo.introduceSpy( "Engelbert", "Humperdinck" ) = "Humperdinck... *Engelbert* Humperdinck." */ |
Before you start writing code, write a two more test cases. What are some degenerate inputs to try? (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.
We will check this function off. For full credit, you'll need
You might say “Libby -- *Lewis Scooter* Libby”. Or, you might say “Scooter Libby -- *Lewis* Scooter Libby”. Choose the approach you prefer, and write a function introduceSpy3, which takes three inputs (first name, middle name, last name), and gives your preferred variant of the answer.
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.
(Challenge problem!) 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( "Lewis", mcJo.introduceSpy( "Scooter", "Libby" ) ) mcJo.introduceSpy( mcJo.introduceSpy( "Lewis", "Scooter" ), "Libby" ) ) |
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. ↩
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 |