RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab02a
lab02a
Strings

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.

A function with two inputs: example

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.
  }
Note that in the comments, we have two lines beginning “@param”, naming our two parameters. For Global thinkers, who want the general rule, here's the syntax for defining simple two-argument functions:
typereturn namefunction( typeparam1 nameparam1, typeparam2 nameparam2 ) {
  return expression;
  }

Write String literals in quotes

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" )
not
jo.echo2( hello )   /* error -- cannot find variable hello */


Today's task

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 is indeed a String value.

As just discussed in the lecture yesterday, +, if applied to Strings, will concatenate them. Try these in the Code Pane:


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.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme