Task: Write a program called PigLatin which repeatedly prompts the user for a wo
ID: 3725287 • Letter: T
Question
Task: Write a program called PigLatin which repeatedly prompts the user for a word and prints out the Pig Latin version of that word. The program should stop when the user enters "stop". (Do not convert "stop" to Pig Latin.)
Steps: Your program must contain the following elements:
A function called isVowel which takes a character (type char) as a parameter and returns true if the character is a vowel and false if it is not. Vowels are the letters a, e, i, o, and u.
A function called findFirstVowel which takes a string as a parameter and returns the position of the first vowel occurring in the string, or -1 if there are no vowels in the string. Counting should start at 0 (like arrays), so the function should return 0 for "apple" and "orange", 1 for "cat" and "dog", 3 for "scratch", and -1 for "xyzzy". The function should go through the characters of the string one at a time and determine if each is a vowel or not. (Do not find some alternative method for locating the position of the first vowel.)
A function called pigifyWord which takes a string representing a single word as a parameter, and returns the Pig Latin version of that string. (Don't print it!)
A main program which repeatedly prompts the user for a word and prints out the Pig Latin version of that word. The program should stop when the user enters "stop". (Do not convert "stop" to Pig Latin.)
Call the various subroutines and functions as appropriate: findFirstVowel should use isVowel to determine if a particular character is a vowel or not, pigifyWord should use findFirstVowel to find the position of the first vowel in the word so you can determine how to pigify it, and main should use pigifyWord to convert each word to Pig Latin.
Explanation / Answer
package piglatiin;
import java.util.Scanner;
public class PigLatin {
public boolean isVowel(char c){
if(c=='a' || c=='A' ||c=='e' || c=='E' ||c=='i' || c=='I' ||c=='o' || c=='O' ||c=='u' || c=='U' )
return true;//the character is a vowel
return false;//the character is not a vowel
}
public int findFirstVowel(String word){
int i=0;
for(;i<word.length();i++){
if(isVowel(word.charAt(i)))
return i;//return the index of first vowel
}
return -1;//No vowel in the word
}
public String pigifyWord(String word){
String pigified="";
int first=findFirstVowel(word);
if(first>0)//The word begins with a consonant
pigified=word.substring(first)+word.substring(0, first)+"ay";
else if(first==0)//The word begins with a vowel
pigified=word+"way";
else//There is not vowel in the word, Look for a y in the word
{
if(word.contains("y"))
//Find the index of y and consider it as a vowel
pigified=word.substring(word.indexOf("y"))+word.substring(0, word.indexOf("y"))+"ay";
}
return pigified;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
while (true) {//Take input from user continously
String word = sc.next();//Take the input from user
if (word.equals("stop")) {
System.exit(0);//Exit the application when user enters stop as input
}
else {
System.out.println(new PigLatin().pigifyWord(word));
//Print the piggified word
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.