Question: Java Lab 2 Pig Latin Program must be in Java - starting code is at the bottom Background Pig latin is a childrens language game

Java Lab 2 Pig Latin

Program must be in Java - starting code is at the bottom

Background Pig latin is a childrens language game where English words are encoded in a simple way. The sentence: do you want a cookie becomes: oday ouyay antway away ookiecay The rules for Pig Latin (at least for this exercise there are variants) are: 1. If a word begins with consonants, move the consonants to the end and add the suffix ay. 2. If a word begins with a vowel (defined as a, e, i, o, and u) simply add the suffix way. 3. If the word does not contain a vowel (ex. cry) do not change the word.

Assignment Download the starter project from Blackboard. Import it into your Eclipse workspace. Implement the following methods to get your Pig Latin translator working: private boolean isVowel(char ch) test the character ch. If it is a vowel, return true, otherwise return false.

private int indexOfVowel(String word) iterate (loop) through all the characters in a word. Return the index of the first vowel in the word. If the word has no vowels return the length of the word. Make use of the isVowel() method you previously implemented.

private String translateWord(String word) translate a word into Pig Latin. Make use of the indexOfVowel() method you previously implemented. Use the following algorithm: If the word begins with a vowel return the word concatenated with the way suffix If the word does not begin with a vowel move the characters preceding the vowel to the end of the word (use the substring() method) and add the suffix ay. Return the resulting word. If the word does not contain a vowel return the word unchanged

Assumptions The program does not do much error checking. It makes the following assumptions: 1. The user will not input any punctuation or numbers. 2. There is one space between each word. 3. The program converts all characters to lower case

Sample Output Example 1: *** Pig Latin Translator *** Enter text to translate: Do not tell mom we ate all the cookies Translation: oday otnay elltay ommay eway ateway allway ethay ookiescay Example 2: *** Pig Latin Translator *** Enter text to translate: Try to translate this string Translation: try otay anslatetray isthay ingstray Example 3: *** Pig Latin Translator *** Enter text to translate: I think pig latin is silly Translation: iway inkthay igpay atinlay isway illysay

Current Code

import java.util.Scanner;

/** * Pig Latin Translator * @author * */ public class PigLatin { private static Scanner input = new Scanner(System.in);

public static void main(String[] args) { PigLatin translator = new PigLatin(); System.out.println("*** Pig Latin Translator ***"); System.out.print("Enter text to translate: "); String text = input.nextLine(); System.out.println("Translation: "+translator.translate(text)); } /** * Translate an entire sentence to Pig Latin * @param text the text to translate * @return translated text */ public String translate(String text) { StringBuilder output = new StringBuilder(); text = text.toLowerCase(); String[] words = text.split(" "); if (words.length > 0) { for (String word : words) { output.append(translateWord(word)); output.append(" "); } } return output.toString().strip(); } /** * Translate a word to Pig Latin * @param word the word to translate * @return translated word */ private String translateWord(String word) { String translated = ""; // TODO - Implement method return translated; } /** * Return the index of the first vowel in the word. Returns the word length if no vowels are present * @param word the word to examine for a vowel * @return the index of the first vowel */ private int indexOfVowel(String word) { int index = 0; // TODO - implement method return index; } /** * Determine if a character is a vowel (a,e,i,o,u) * @param ch character to test * @return true if the character is a vowel, false otherwise */ private boolean isVowel(char ch) { // TODO - delete next statement and implement this method return true; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!