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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab14a
iteration
while vs for

For this lab, we'll make a class Linguistics. We won't have any fields inside the class.
  1. Write a function countVowels, which takes a String, and returns how many vowels it has. Use a while-loop.
    1. Here are some test cases you can paste into the (khaki green) unit-test class:
      assertEquals( 0, Linguistics.countVowels( "" ) );
      assertEquals( 0, Linguistics.countVowels( "z" ) );
      assertEquals( 0, Linguistics.countVowels( "zxyz" ) );
      assertEquals( 1, Linguistics.countVowels( "a" ) );
      assertEquals( 1, Linguistics.countVowels( "A" ) );
      assertEquals( 0, Linguistics.countVowels( "y" ) );
      assertEquals( 0, Linguistics.countVowels( "Y" ) );
      assertEquals( 5, Linguistics.countVowels( "aeiou" ) );
      assertEquals( 5, Linguistics.countVowels( "AEIOU" ) );
      assertEquals( 1, Linguistics.countVowels( "Zyzzyva" ) );
      assertEquals( 4, Linguistics.countVowels( "Test Cases, Eh?" ) );
      assertEquals( 5, Linguistics.countVowels( "abcdefghijklmnopqrstuvwxyz" );
      assertEquals( 0, Linguistics.countVowels( "$%^#!" ) );
              
      Note that this is not an unusually large set of test cases, in real-world terms.
    2. For good style, first define a named-constant VOWELS to be the "AaEeIiOoUu".
    3. One of the questions we ask ourselves, when writing a loop, is “how do I do a small task once”? In this case, that's “how do I tell if one particular character is a vowel”?

      You should be able to answer this question after perusing standard Java documentation for Strings. Here is what you'd end up with: an expression that tells whether the 17th character of a String s is a vowel:

                VOWELS.contains( new Character( s.charAt(16) ).toString() )
              
      Note that we have to convert a single character into a String (which is a sequence of 0-or-more-characters). We do this by using the wrapper class Character, and its toString method.

    4. Often when writing code, you refer to similar functions, just to get the syntax correct. See below for a while-loop (from lecture) which addes all the ages Dogs in a certain list.
    5. Optional, but easy: It's a pain that our named constant has to include both upper- and lower-case versions. Modify your code so that if you decide to count 'y' as a vowel, you only have to add one character to VOWELS, not two
    6. Optional, if you have finished the entire lab:
      Make a second method which tells how many characters of a String occur in some other “reference” String. For example, how many letters occur in the word "creations" (which happens to have all the most common letters in English).
      Make sure that when you're done, your vowel-counting function is just a single line.
  2. Modify your method so that it uses a for loop (not a for-each loop). Yikes, we haven't talked about these old-school for loops! They turn out to be handy when you want a loop-variable (an index) which just counts from one number to another (e.g. 0 to n, or a to b). Details of the official syntax for the for-loop is on the lecture notes; we'll review it Thurs.
  3. Optional: write a method which takes in a string, and determines whether it's a palindrome -- whether it reads the same forwards and backwards. For instance, if you are ignoring case, "ava" and "MadamImAdam" are both palindromes1.

1 If you really want to be fancy, you can ignore spaces and punctuation, so that you'd correctly classify the palindromes “Able was I, ere I saw Elba.” and “A man, a plan, a canal -- Panama!"”. In my own version of this punctuation-ignoring version, I find a while loop very natural to use, (and a for loop awkward), because it's no longer clear in advance exactly how far through the String I'll look at. We'll talk more in lecture about when to use each.      

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