RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab02a
Spies and MCs
Javadoc; Strings

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

Today's project will be done in pairs.

Background: 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. 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.
  }
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;
  }

Javadoc

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.

Javadoc: Full options, and tips from the experts.

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!

Reminder: put 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. "hi" + "there"
  2. "Ja" + "va a la" + "ng" + "u" + "age."
  3. Write a method which, given any name, will return an introduction for that name. That is:
    /** 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.)
    
    The standard steps of approaching a problem:
    1. Test cases. Three are already provided for you; paste those into your code, and write at least one of your own
    2. Write the signature of the function: remember the mantra for declaring a function, “returnType-functionName-openParen-paramName&type”
    3. Javadoc comments. Explain the meaning of each parameter, and what is returned.
    4. Finally, fill in the body of the function (return-expression-semicolon).
    5. Test your function by calling the actual test cases written above.
  4. Test introduce by creating an instance of class Emcee (by right-clicking on the tan class-definition-box). Name it, say, “mcJo” (or whatever you like). Then, call your functions in two different ways:
  5. 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).

  6. Lectroid spies all have the first name John. Write a program introduceLectroidSpy which takes in only one input, a last name, and introduces the spy. Make one additional test case:
    /**
     *     ... 
     *
     * Test cases:
     * introduceLectroidSpy( "Parker" ) = "Parker... *John* Parker."
     * introduceLectroidSpy( "Z" ) = _________
     */
    
        // After test cases, write the signature here.
    
  7. Test your method.

Solution

Challenge problem (optional)

Some people wonder, how Bond's standard introduction can be modified for people with three names — say, 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.)      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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