Write your code in the file PigLatin.java. Your code should go into a method wit
ID: 3937391 • Letter: W
Question
Write your code in the file PigLatin.java. Your code should go into a method with the following signature. You may write your own main method to test your code. The graders will ignore your main method: public static String translate (String original){} "Pig Latin" is a fake language used as a children's game. A word in English is "translated" into Pig Latin using the following rules: If the English word begins with a consonant, move the consonant to the end of the word and add "ay". The letter Y should be considered a consonant. If the English word begins with a vowel (A, E, I, O, or U), simply add "way" to the end of the word. (This is a simplified dialect of Pig Latin, of course.) Write your method so that it returns the pig latin translated original string. You may assume that the input does not contain digits, punctuation, or spaces. The input may be in any combination of uppercase or lowercase. The case of your output does not matter.
Explanation / Answer
PigLatin.java
import java.util.Scanner;
public class PigLatin {
public static String translate(String original){
original = original.toLowerCase();
String vowels = "aeiouAEIOU";
if (vowels.contains(""+original.charAt(0)))
original = original + "way";
else{
original = original.substring(1, original.length())+ original.charAt(0) + "ay";
}
//System.out.println(original);
return original;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word: ");
String original = scan.next();
String newString = translate(original);
System.out.println("PigLatin String is "+newString);
}
}
Output:
Enter a word:
suresh
PigLatin String is ureshsay
Enter a word:
apple
PigLatin String is appleway
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.