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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect13b
finishing loops

The canonical example of a for loop:

  for (int i = 0; i < N; ++i) {
    // ... body involving i...
    // We'll give an example using System.out.println,
    // although printing doesn't actually help accumulate
    // any answer worth returning.
    }
Exercise: Write versesFromTo (a.k.a. manyVerses) to use a for loop.

Given a list of Treasures, loot,

  1. create a list of all items which weigh less than 20, using a for-each loop.
  2. As above, but using a while-loop (and the List method get).
  3. As above, but using a for loop.
  4. A variant on the above: add a debugging statement which prints a message to the console window every time an item is added to the result. (What is helpful to have printed, to understand your program?)
    Remember that printing to the console is not the same as returning a value. The “results” of printing to the console are void; they can't be used to play nicely with any other function.
    (A common hw10 error: thinking that exploreOnce returns a message.)
  5. Determine whether any Treasure in loot is lint, using a for-each loop (and examining every item in the list).
  6. As above, but using a while-loop . (You will still examine every item in the list, for now.)
  7. As above, but using a for loop.
  8. Modify your last two versions so that the loop ends as soon as you find a match. (Note that when using a for-each loop, there is no natural way1 to stop early.)

Here is another problem worked on in some of the lectures:

    
    
  /** Return all Strings in a list which start with a given prefix.
   * @param msgs The list of Strings to search through.
   * @param prefix The string to look for, as a prefix.
   * @return all Strings in msgs which start with prefix.
   */
  public static LinkedList<String> allMatches( String prefix, LinkedList<String> msgs ) {
    LinkedList<String> matchesSoFar = new LinkedList<String>();
    
    for ( String m : msgs ) {
      if (startsWith(m , prefix)) {
        matchesSoFar.add(m);
        }
      }
    return matchesSoFar;
  }
This was easy to write, except that we just wished that such a method startsWith existed. That's okay; we can write it ourselves! Let's do that, for more loop experience. Here's a version which doesn't call substring:
  /** Does one string have another as a prefix?
   * @param text The string to look at the beginning of
   * @param prfx The string to try to match at the start of text.
   * @return Does text start with the characters inside prfx?
   */
  private static boolean startsWith( String text, String prfx ) {

    if (text.length() < prfx.length()) {
      return false;
      }
    else {
      boolean allMatchSoFar = true;
      int i = 0;
    
      while (i < prfx.length()) {
        if (prfx.charAt(i) != text.charAt(i)) {
          allMatchSoFar = false;
          }
        i = i+1;
        }
        
      return allMatchSoFar;   

      // Alternately: replace the `if` with:
      // allMatchedSoFar &&= (prfx.charAt(i)==text.charAt(i));

      // Alternately: replace the entire while-loop with
      //   return text.substring(0,prfx.length()).equals(prfx)
      // (Note that substring and equals each have loops internally.)
      }
    
    }


1Using return from the middle of a loop is poor style. And using break and continue are unaesthetic, which is why we won't talk about them in class, although you're free to read the book.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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