|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.
/** 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`. */ |
/* 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" ) ); |
nextIndexToCheck1 | something-so-far |
0 | 0 |
1 | 0 |
2 | 0 |
3 | 1 |
4 | 2 |
5 | 2 |
... | ... |
... | ... |
... | ... |
... | ... |
/** 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`. */ |
/* 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" ) ); |
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( "$%^#!" ) ); |
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?
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. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Nov.14 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |