Page 461 - Cs_withBlue_J_C11_Flipbook
P. 461
Program 3 Write a program to create a package Eleven containing class Vowel having the following
specifications.
Class Name : Vowel
Package name : Eleven
int firstvowel(String w) : Returns the position of the first occurrence of vowel in
word w in general. Returns -1 if the word does not have
any vowel and 0 if the words start with a vowel
The rules used by Pig Latin are as follows:
• If a word begins with a vowel, add “YAY” to its end. For example, “ANT” is translated into
“ANTYAY”.
• If it begins with a consonant, then first write all the characters from the first vowel to the
end of the word, then add characters preceding the vowel followed by “AY”. Simply, we
take all the consonants before the first vowel and we put them on the end of the word
followed by “AY”. For example, “JAVA” becomes “AVAJAY”.
• If the word has no vowel, then it is written unchanged like SKY remains SKY.
Class has the following class description. It calls method int firstvowel(String) of the class.
Class name : Piglatin
Package : Orange_prog
Data Members
String s : To store sentence
void accept() : Accepts sentence in variable ‘s’
void display() : Extracts each word from the sentence and using method
int firstvowel(String) of class Vowel belonging to package
Eleven print each word in Pig Latin form.
The class Vowel belonging to package Eleven from another class Piglatin in another package
Orange_prog and prints the words of a sentence in Pig Latin form.
1 package Eleven; // declaring package
2 public class Vowel
3 { public int firstvowel(String w) // returning first occurrence of vowel
4 { int pos=-1,l,i;
5 l=w.length();
6 for(i=0;i<l;i++)
7 {
8 char ch=w.charAt(i);
9 if("AEIOU".indexOf(ch)!=-1)
10 {
11 pos=i;
12 break;
13 }
14 }
459
Packages 459

