We'll experiment with Strings, which were barely introduced in lecture yesterday.
Today's project can be done individually, or in a pair.
Preliminary:
class StringsInLab { } |
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 first few minutes of the Lab section, +, if applied to strings, will concatenate them. Try these in the Code Pane:
/* Return the results heard when shouting `shoutOut' near a cliff. * echo( "Ahoy" ) = "AhoyAhoy" * echo( "" ) = "" */ String echo( String shoutOut ) { return shoutOut + shoutOut; } |
/* echo2( "Ahoy" ) = "Ahoy! ...Ahoy, Ahoy" |
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: * spyIntroduction( "Dweezil", "Zappa" ) = "Zappa... *Dweezil* Zappa." */ |
Before you start writing code, write a few 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.)
Hmmm, this is new -- a function which accepts two
inputs, we haven't seen that before!
Well, give it a whirl anyway.
If you like, here's a syntax rule for defining simple two-argument functions:
[return-type] [name]( [type] [arg1name], [type] [arg2name] ) { return [expression-involving-arg1name-and-arg2name]; } |
If you finish these tasks, you are free to start working on hw02i (in a new BlueJ project window), and/or line up a partner for hw02g.
Yesterday in class we say that when you write 3+"musketeers",
Java realizes that neither meaning of + (adding numbers, and
concatenating strings) makes any sense, and as a (perhaps confusing) courtesy
it inserts a function call which converts the 3 into
a String for you.
After Java does that, string concatenation makes sense, so:
3 + "musketeers"
= Integer.toString(3) + "musketeers"
= "3" + "musketeers"
= "3musketeers"
Not only does Java provide a function + to concatentate strings, but it also provides a function to extract pieces of a string: substring. Try the following, in BlueJ's codepane:
String menuEntry = "hamburger"; menuEntry.substring(1,3) |
Okay, but if we start counting at 0, then the character at index three is 'b', so why didn't the returned string include 'b'?
Because it turns out, the function substring( int start, int stop ) returns the letters starting at index start, up to (not including) index stop.
The only way you could have known this was by reading the documentation (comments) for substring. This is a live example of why comments are so important!
Much more unsettling -- if this is a function which takes a string, a start-point, and an end-point, then why didn't we write
substring(menuEntry, 1, 3) /* Does not work?! */ |
We've actually seen this before, when using BlueJ: in the first week, we created a PizzaServer object, we named it jo, and then (by clicking in BlueJ) we asked jo.pieArea(20). In fact, in the previous part of this very lab, when we tested spyIntroduction, we made objects: From the template class StringsInLab, we made a new object (an “instance”) and associated it with the identifier (say) stringsInLa1. Then we walked up to stringsInLa1 and asked it “hey, give me spyIntroduction( "James", "Bond" ).” Look closely at the first words BlueJ's results-window:
stringsInLa1.spyIntroduction("James", "Bond") returned: … |
Note that you don't need to name every String object, you can walk up to any String and ask it for substrings
"hamburger".substring(4,8) "smiles".substring(1,5) |
Today's project can be done individually, or in a pair. Before leaving, you'll need to check off your work (see below).
Evaluate each of these in BlueJ's code pane. Before hitting return, make a guess to yourself (or your partner), what you think your answer will be:
Although we won't use substring in a program yet, go ahead and try these out:
Now, we are ready for another short program:
You are working for a pet store, and you need to write a function which takes two pieces of information: an animal type, and the number of those animals currently in stock; it returns a string conveying that information.
inventoryMessage( "dog", 3 ) = "There are 3 dogs in stock." |
If you finish this, you can leave early, or begin working on hw02g.
1 If you don't see such a pane, select View > Show Code Pane. back
2 I don't suggest using this introduction at real-life cocktail parties; it only works in movies. back
3In BlueJ's code pane, if you press the up-arrow, you'll be able to easily repeat/modify your previous expression. back
Ian's home page | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu | Last modified 2006.Sep.01 (Fri) |