HI i need help with this code. It\'s need to follow specific guidelines. Please
ID: 3778645 • Letter: H
Question
HI i need help with this code. It's need to follow specific guidelines.
Please follow the guidelines very carefully. The words that are in bold are VERY important and needs to be implemented into this code.
Write a program that converts words to the CS1 dialect of Pig Latin . This program has several steps. To begin, you will first implement an isVowel method, which takes as arguments a character (the input to be tested) and a Boolean flag, which tells you whether or not ‘Y’ should be counted as a vowel – your method should return true if the input is a vowel, whether it is in upper- or lower-case.
Next, write the pigLatin method, which accepts a single String argument and returns the Pig Latin translation as a String. Here are the basic rules for our dialect:
1. If the first letter in the word is in (A, E, I, O, U), add “way” to the end
2. Otherwise, find the first letter (left-to-right) that is in (A, E, I, O, U, Y), move all characters before it to the end of the word, and append “ay”
a. If the first letter of the word was capitalized, you should make sure that the first letter of the translation is capitalized, and that the moved first letter is made lower-case (no other capitalization changes should be made)
Finally, write the main method, which should prompt the user for a word, translate it into Pig Latin, and repeat until the user enters “exitway” (“exit” in Pig Latin; any case should be acceptable). Consider the following example run of the program:
Enter a word to translate (exitway to finish): Extra
Extraway
Enter a word to translate (exitway to finish): Credit
Editcray
Enter a word to translate (exitway to finish): eXitwaY
To write this program you may find it useful to reference the Oracle documentation for the String class , which has several useful methods. In particular, see the substring and toUpperCase/toLowerCase methods, in addition to the length, equals, and charAt methods we’ve covered in class.
Explanation / Answer
PigLatin.java
import java.util.Scanner;
public class PigLatin {
public static String pigLatin (String original){
original = original.toLowerCase();
if (isVowel(original.charAt(0)))
original = original + "way";
else{
int i = 0;
for(i=0; i<original.length(); i++){
if(isVowel(original.charAt(i)) || original.charAt(i) == 'y' || original.charAt(i) =='Y'){
break;
}
}
original = original.substring(i)+ original.substring(0,i) + "ay";
}
//System.out.println(original);
original = Character.toUpperCase(original.charAt(0))+original.substring(1);
return original;
}
public static boolean isVowel(char ch){
String vowels = "aeiouAEIOU";
if (vowels.contains(""+ch)){
return true;
}
else{
return false;
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter a word to translate (exitway to finish): ");
String original = scan.next();
while(!original.equalsIgnoreCase("exitway")){
String newString = pigLatin (original);
System.out.println(newString);
System.out.print("Enter a word to translate (exitway to finish): ");
original = scan.next();
}
}
}
Output:
Enter a word to translate (exitway to finish): Extra
Extraway
Enter a word to translate (exitway to finish): Credit
Editcray
Enter a word to translate (exitway to finish): eXitwaY
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.