|
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.
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; } |
You've (hopefully) noticed in the code presented in class, the function is preceded by comments, and the comments have kinda a funny format, what with those “@” signs. The comments are in a special format, called “javadoc”. They explain what the parameters numPizzas and size mean, and what the returned answer means. They help people understand your program, without having to read the bare code.
There is a bonus to writing this information using those tags @param and @return: In BlueJ, look at the code window, and in the upper-right corner change Implementation view (Source Code in BlueJ v2.2.0) to instead ask for the Interface view (Documentation in BlueJ v2.2.0). Voila! Other programmers can look at this web page for pizzaServer, and know how to call our functions, without ever actually having to look at our code!
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 { // (We will add all our functions inside the class's curly-braces.) } |
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( ____ ) = ______________________________ * mcJo.introduce( "" ) = "Hello, world; my name is ." */ // Put signature here. (See the javadoc, for the parameter name.) |
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:
/** * ...Add 'param' and 'return' javadoc tags as needed. * * Test cases: * * mcJo.introduceSpy( "Ian", "Barland" ) = "Barland... *Ian* Barland." * mcJo.introduceSpy( "Dweezil", "Zappa" ) = "Zappa... *Dweezil* Zappa." * mcJo.introduceSpy( "Engelbert", "Humperdinck" ) = "Humperdinck... *Engelbert* Humperdinck." * mcJo.introduceSpy( "McLovin", "" ) = ______________ * mcJo.introduceSpy( "", "Feist" ) = ______________ * mcJo.introduceSpy( "", "" ) = ______________ */ _______ introduceSpy( _____ ______, _____ _____ ) { return ________________________________________; } |
Before you start writing code, write 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 (including the test case and the javadoc).
/** * ... * * 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.
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.
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. ↩
3The right-associative version, or the left-associative? (resp.) ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Nov.26 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |