Radford University ITEC

ITEC120

lab02:
Strings

Aug.30 (Wed)

We'll experiment with Strings, which were barely introduced in lecture yesterday.

Today's project can be done individually, or in a pair.

Preliminary:

Experimenting on Strings

Go to BlueJ's code pad in the lower-right corner of the main screen1. Type in some sample strings, such as:

Nothing too exciting; in each case, BlueJ confirms that the expression you typed in indeed is a String value.

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:

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.


Sep.01 (Fri)

A preview to objects

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)
What's going on?! Several things are a bit odd:

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)

hands-on exercise

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.

Before you leave, check off your work with one of the instructors.

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)