1. Trace through execution of String manipulation procedure
2. Boolean variables and procedures
Tracing through execution of String manipulation procedures
The following slides show the step-by-step execution of the procedure countCharacter:
Execution of countCharacter
Boolean variables and procedures
Certain procedures have the purpose of returning only a "Yes"/"No" answer. This
is very common and Java has a special type, called boolean, which can be used to
indicate that a procedure only returns "Yes"/"No" or that we need a variable that
only allows "Yes"/"No" to be written on it.
Keep in mind that in Java "Yes"/"No" is written as true / false (no quotes around).
Typically the name of a boolean procedure is phrased as a question to suggest that
it is a boolean procedure
Searching for a character in a string (boolean procedure)
Goal: Write a procedure hasVowel(phrase) that takes a string and displays
determines whether the given string contains a vowel.
For example:
hasConsonant( "hello" ) should return true
hasConsonant( "aeiou" ) should return false
hasConsonant( "" ) should return ???
Note that:
- this is a boolean procedure since it effectively reports only "Yes"/"No"
- its name is phrased as a question to suggest that it is a boolean procedure
The structure of hasConsonant is similar to that of countCharacter:
1. for each valid index in the given phrase:
2. extract the current character
3. if the current character is a consonant:
4. report success
5. report failure
Here is the translation in Java. There are two new elements:
- the type of the procedure is the new boolean type
- the return is used inside an if -- whenever we are confident
that we have the result, we can use return directly to stop
the procedure and give the result back
boolean hasConsonant( String phrase )
{
for ( int index = 0 ; index < phrase.length() ; index = index + 1 ) // (Step 1)
{
char curLetter = phrase.charAt( index ); // (Step 2)
if ( curLetter == 'b' || curLetter == 'c' || curLetter == 'd' ... ) // (Step 3)
{
return true; // (Step 4)
}
}
return false; // (Step 5)
}
INCORRECT solution
Here is a typical incorrect solution. It commits to "Yes"/"No" answer right away,
so if the first character is a vowel the procedure will attempt to return "No"
immediately. Even though there is a loop, exactly only cycle will be executed
every single time.
boolean hasConsonant( String phrase )
{
for ( int index = 0 ; index < phrase.length() ; index = index + 1 ) // (Step 1)
{
char curLetter = phrase.charAt( index ); // (Step 2)
if ( curLetter == 'b' || curLetter == 'c' || curLetter == 'd' ... ) // (Step 3)
{
return true; // (Step 4)
}
else
{
return false; // WRONG: cannot return answer yet // (Step 5)
}
}
}
Checking if a character is a consonant (boolean procedure)
We did not complete the procedure hasConsonant since the if statement needs 21
separate comparisons (one for each consonant)
To simplify matters we can write a procedure isVowel to determine if a character
is a vowel. Then we can use it to write a simple procedure isConsonant
Both are examples of boolean procedure.
boolean isVowel( char letter )
{
if ( letter == 'a' || letter == 'e' || letter == 'o' || letter == 'i' || letter 'u' )
{
return true;
}
else
{
return false;
}
return result;
}
|
boolean isConsonant( char letter )
{
if ( isVowel( letter ) == false ) // if NOT vowel
{
return true; // report YES, must be consonant
}
else
{
return false; // report NO, was vowel
}
}
|
boolean isConsonant( char letter )
{
if ( isVowel( letter ) == true ) // if IS vowel
{
return false; // report NO, not consonant
}
else
{
return true; // report YES, was consonant
}
}
|
Using boolean procedures
We can use the boolean procedure
isConsonant to simplify the code for
hasConsonant:
boolean hasConsonant( String phrase )
{
for ( int index = 0 ; index < phrase.length() ; index = index + 1 )
{
char curLetter = phrase.charAt( index );
if ( isConsonant(curLetter) == true )
{
return true;
}
}
return false
}