0% found this document useful (0 votes)
6 views5 pages

Is Vowel Functions

The document outlines various methods to determine if a character is a vowel in English, suggesting multiple implementations for the 'isVowel' method using different programming techniques. It also includes examples of how to call this method in both JJS and Java, along with additional tasks for creating methods to detect digits, punctuation, letters, and more. The document emphasizes familiarity with string manipulation and encourages the creation of a character utility class.

Uploaded by

neeluvishwanath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Is Vowel Functions

The document outlines various methods to determine if a character is a vowel in English, suggesting multiple implementations for the 'isVowel' method using different programming techniques. It also includes examples of how to call this method in both JJS and Java, along with additional tasks for creating methods to detect digits, punctuation, letters, and more. The document emphasizes familiarity with string manipulation and encourages the creation of a character utility class.

Uploaded by

neeluvishwanath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Problems with Vowels: on Strings and methods

Vowels in English are the five characters, A, E, I, O and U .


To determine whether a character is a vowel can be done in a
great many ways (about half a dozen different ways).

Create a method isVowel which acts on a character ch


(represented as type String) and returns true when this
character is a vowel, either upper case or lower case.
Do this in many ways, to get familiar with strings, and then
chose one that you prefer. Note that toUpperCase can
simplify some versions considerably.

Suggested ways are:

a. Use a huge “nice” nested If-ElseIf.

b. Use a series of Choices each having a logical Or operator

c. Use a single logical Or with a rather complex condition

d. Use the substring command and String vowel = "AEIOUaeiou".

e. Use an array of the 5 vowel strings and toUpperCase.

f. Use the String vowel = "AEIOU" and indexOf. 1

// The isVowel method can be called as follows in JJS

public static void main (String[] args) {


String str;
JJS.outputlnString ("Enter a character ");
st = JJS.inputString();
JJS.outputlnString (st);
JJS.outputString ("It is ");
JJS.outputlnBoolean (isVowel(str));
JJS.outputlnString ("that it is a vowel");
JJS.outputString ("\n"); //gap
}// end void method main

// The isVowel method is called in full Java as follows


// but must be defined as static

public static void main (String[ ] args) {


String str;
// str = “A”;
str = "b";
System.out.print (It is ");
System.out.println (isVowel(str));
System.out.print (“that ” + str + “ is a vowel");
}//end method main 2
// isVowel implemented with nice nested choices

public static boolean isVowel (String chr) {


boolean result;
String ch;
// Need: chr is of length 1
ch = chr.toUpperCase();
if (ch.equals("A")) {
result = true;
}else if (ch.equals("E")) {
result = true;
}else if (ch.equals("I")) {
result = true;
}else if (ch.equals("O")) {
result = true;
}else if (ch.equals("U")) {
result = true;
}else{
result = false;
}//end if
return result;
}//end boolean method isVowel

// isVowel implemented with series of Choices

public static boolean isVowel (String chr) {


boolean result;
String ch;
// Need: chr is of length 1
ch = chr.toUpperCase();

result = false;

if (ch.equals("A")) {
result = true;
}//end if

if (ch.equals("E")) {
result = true;
}//end if

if (ch.equals("I")) {
result = true;
}//end if

if (ch.equals("O")) {
result = true;
}//end if

if (ch.equals("U")) {
result = true;
}//end if

return result;
}//end boolean method isVowel
4
// isVowel implemented with a complex Choice

public static boolean isVowel (String chr) {


boolean result;
String ch;
// Need: chr is of length 1
ch = chr.toUpperCase();
result = false;
if ( (ch.equals("A")) ||
(ch.equals("E")) ||
(ch.equals("I")) ||
(ch.equals("O")) ||
(ch.equals("U")) ){
result = true;
}//end if
return result;
}//end boolean method isVowel

// isVowel implemented with substring

public static boolean isVowel (String chr) {


boolean result;
String vowel, subChr;
// Need: chr is of length 1
// Does: return status of chr
vowel = "AEIOUaeiou";
result = false;
for (int i=0; i < vowel.length(); i++) {
subChr= vowel.substring (i, i+1);
if (chr.equals (subChr)) {
result = true;
}//end if
}//end for
return result;
}//end boolean method isVowel

6
// isVowel implemented with an Array

public static boolean isVowel (String chr) {


boolean result;
String vowel, subChr;
// Need: chr is of length 1
// Does: return status of chr
String vArray[] = {"A", "E", "I", "O", "U"};
chr = chr.toUpperCase();
result = false;
for (int i=0; i < vArray.length; i++) {
if (chr.equals(vArray[i]) ){
result = true;
}//end if
}//end for
return result;
}//end boolean method isVowel

// isVowel implemented using indexOf method

public static boolean isVowel (String chr) {


boolean result;
String vowel;
// Need: chr is of length 1
// Does: return status of chr
vowel = "AEIOUaeiou";
result = (vowel.indexOf(chr) >= 0);
return result;
}//end boolean method isVowel

8
Other Problems Involving Text (String Characters)

1. Create the following methods involving String characters.

a. isDigit, which detects any of the ten decimal digits

b. isPunctuation, which detects punctuation, such as


periods, colons, semicolons, etc

c. isLetter, which tells if a character is a letter,


either upper case or lower case

d. isCap, which tells if a character is an upper case letter

e. isOp, which indicates if a character is an arithmetic operation

f. capped, which returns the capital letter

2. Create some other useful methods similar to the above, and


put them into a Chr class, along with some useful constants
(period, space, hyphen, dash, etc) to be similar to the Math
class.

10

You might also like