Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that lets you take a word and convert it to \'Pig Latin\'. The r

ID: 3678515 • Letter: W

Question

Write a program that lets you take a word and convert it to 'Pig Latin'. The rules of conversionare as follows:        

Take the letters up to the first index of a vowel and move it to the end of the word. Then add an"ay" on the end. If the word starts with a vowel, then add "yay" to the end of the word.

Required Methods:

getFirstVowelIndex: Pass in the word as a parameter. Then check for 'a', 'e', 'i', 'o', 'u' (treat 'y' as a consonant so don't check for it). For each of the five vowels, keep track of the smallest index that isn't -1 (check for this before comparing a vowel's index against the current index). It's possible that there are no vowels but very rare so go ahead and ignore this possibility.

getPigLatinWord: Pass in the word as a parameter. Passes back the "pig latin" word. This method will call the getFirstVowelIndex method.

Example run:

run:

Enter word:  hello

word is: hello

The pig latin word is: ellohay

BUILD SUCCESSFUL (total time: 2 seconds)

===========================

Enter word:  armor

word is: armor

The pig latin word is: armoryay

========================

run: Enter word:  throat

word is: throat

The pig latin word is: oatthray

BUILD SUCCESSFUL (total time: 14 seconds)

Use indexOf to get the index of a vowel and use Math.min to get the minimum of the current index against a vowel's index.

Explanation / Answer

import java.util.Scanner;
public class PigLatin{

public static void main(String []args){
String result;
long startTime = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.print("Enter your word: ");
String word = sc.nextLine();
while (!word.equalsIgnoreCase("done"))
{
String beforVowel = "";
int cut = 0;
while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
{
beforVowel += word.charAt(cut);
cut++;
}
result=word.substring(cut) + beforVowel + "ay";
if (cut == 0)
{
cut = 1;
result= word + "yay";
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Word is: "+word);
System.out.println("The pig latin word is: "+result);
System.out.println("BUILD SUCCESSFUL(Total Time: "+totalTime +" seconds)");
System.out.println("===========================");
System.out.print("Enter your word:");
word = sc.nextLine();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote