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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab13a
iteration
counting char

For this lab, we'll make a class Linguistics. It will be a utility class, with static methods -- we won't have any fields inside the class. By the end of lab, you should have 1 and 2 finished entirely, and most of #3.

  1. We will write a function countCharIn, which reports how many times a given character occurs within a given string.
      Make a stub function for this.
    1.   /** How many times is a given character contained in a given String?
         * @param target The character to search for.
         * @param src    The string to look for `target` in.
         * @return how many times `target` occurs in `src`.
         */
      
          
    2. Make test cases. One way is to interactively call things in BlueJ. But after a while, you'll find it's easier to write them by hand.
      1. Start recording a test case, but then immediately declare it finished.
        This causes BlueJ to add (an empty) test method inside the unit-test case.
      2. Usually you would start brainstorming your own test cases. For lab, they're provided for you (but observe how each test case checks for a fundamentally different situation than any other). Paste the following into the unit-test function you just created:
                /* Test with empty string, and length-1 strings: */
                assertEquals( 0, Linguistics.countCharIn( 'a', "" ) );
                assertEquals( 0, Linguistics.countCharIn( 'b', "z" ) );
                assertEquals( 0, Linguistics.countCharIn( 'a', "A" ) );
                assertEquals( 1, Linguistics.countCharIn( 'a', "a" ) );
              
                /* Test with longer strings: no occurrence, one occurrence, multiple. */
                assertEquals( 0, Linguistics.countCharIn( 'a', "zyxyz" ) );
                assertEquals( 1, Linguistics.countCharIn( 'x', "zyxyz" ) );
                assertEquals( 2, Linguistics.countCharIn( 'y', "zyxyz" ) );
              
                /* Test longer strings, emphasizing matches at first and last char. */
                assertEquals( 1, Linguistics.countCharIn( 'a', "abcde" ) );
                assertEquals( 1, Linguistics.countCharIn( 'e', "abcde" ) );
              
                assertEquals( 3, Linguistics.countCharIn( 'z', "zazayza" ) );
                assertEquals( 3, Linguistics.countCharIn( 'a', "zaxayza" ) );
                assertEquals( 3, Linguistics.countCharIn( 'z', "zzz" ) );
                
    3. Before jumping into code, Let's work through one example by hand.
      1. As for every loop: What sort of so-far information are we accumulating?
      2. Since we don't have a list, we can't use a for-each loop. That means we'll need to use while (or for). As we saw with counting bowling-pins-in-a-triangle or counting-oranges-in-a-pyramid, sometimes we also need a variable to keep track of how far we are through the problem. For countCharIn, how will we represent how far we are through the problem?
      3. Fill in the following table, keeping track of information as you work through the problem by hand:
        countCharIn( 'w', "Powwow whoa!" )
        nextIndexToCheck1 something-so-far
        00
        10
        20
        31
        42
        52
        ......
        ......
        ......
        ......
      4. We realize our code will need to extract an individual character out of a String (hint: check out documentation for the String method charAt.) We can compare characters like any primitive type: using ==. (Try typing in Code Pad: 'W' == 'w' or 'w' == 'w'. Note the single-quotes, to specify a char literal.)
      You are encouraged to look back at the lecture notes, to see how we solved similar problems.
    4. Now, think about writing the actual code. Be aware of
      1. What task am I doing over and over?
      2. How do I update my accumulated info (the “so-far” variable) in light of the one new piece of information I'm looking at this one time through the loop?
      3. How will I know when I'm done?
  2. Write a method
      /** Is a given character contained in a given String?
       * @param target The character to search for.
       * @param src    The string to look for `target` in.
       * @return whether `target` occurs in `src`.
       */
       
        
    Hint: can you use the function you just wrote? This should be a one-liner! We'll discuss potential drawbacks of this in class tomorrow.
      /* Test with empty string, and length-1 strings: */
      assertEquals( false, Linguistics.charContainedIn( 'a', "" ) );
      assertEquals( false, Linguistics.charContainedIn( 'b', "z" ) );
      assertEquals( false, Linguistics.charContainedIn( 'a', "A" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'a', "a" ) );
    
      /* Test with longer strings: no occurrence, one occurrence, multiple. */
      assertEquals( false, Linguistics.charContainedIn( 'a', "zyxyz" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'x', "zyxyz" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'y', "zyxyz" ) );
    
      /* Test longer strings, emphasizing matches at first and last char. */
      assertEquals( true,  Linguistics.charContainedIn( 'a', "abcde" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'e', "abcde" ) );
    
      assertEquals( true,  Linguistics.charContainedIn( 'z', "zazayza" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'a', "zaxayza" ) );
      assertEquals( true,  Linguistics.charContainedIn( 'z', "zzz" ) );
            
  3. 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( 2, Linguistics.countVowels( "Aa" ) );
      assertEquals( 2, Linguistics.countVowels( "Aba" ) );
      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 "AaEeIiOoUu".
    3. Follow all the steps you used to solve #1. (including writing test cases (your own, this time!), doing one example by hand, etc..)

      One of the questions we ask ourselves, when writing a loop, is “how do I do a small task once”? In this case, How do I count the occurrences of 'A', in a given String? (Easy, using our method above.) But I also want to do this for 'a', and for 'I', and for 'i', ...and for each character someVowel inside the String VOWELS. How can I set up a loop, to do something for each character inside VOWELS?

    4. 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
    5. Optional, if you have finished the entire lab:
      Make a second method which tells how many characters of one String appear in some other “reference” String. For example, numOccurrences( "tarry", "creations" ) returns 4 (since everything but the last 'y' appears in "creations").
      Make sure that after you write this more general function, you factor out common code so that countVowels is only one line long.2

1Other plausible names include “i” and “numCharsAlreadyChecked”. Note that because of how indices start at zero, the number-of-characters-already-checked is the same as the index-of-the-next-character-to-check.      

2Alternately, you can get rid of countVowels altogether, just calling your new function with VOWELS as the second argument. This entails a search-replace in your test-class, so it no longer mentions the outdated function.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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