Question

In: Computer Science

Write a program that translates an English word into a Pig Latin word. Input ONE, and...

Write a program that translates an English word into a Pig Latin word. Input ONE, and ONLY one, word from the user, and then output its translation to Pig Latin.

The rules for converting a word to Pig Latin follow.

  1. The general form for converting a word is to remove the first letter. Then append to the end of the word a hyphen, followed by the first letter that was removed, followed by "ay". For example, "robot" becomes "obot-ray" and "Hello" becomes "ello-Hay".
  2. If the word begins with a vowel (an element of "aeiouAEIOU") simply append "-way". For example, "example" becomes "example-way" and "All" becomes "All-way".
  3. If the word begins with "th" (or "Th", "tH", or "TH"), remove the prefix and append "-thay" (or "-Thay", "-tHay", or "-THay") to the word. For example, "this" becomes "is-thay" and "The" becomes "e-Thay".

Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters.

Input Specification

Input ONE English word as a string. If multiple words are entered ignore any beyond the first.

Use PRECISELY the format below with the EXACT same SPACING and SPELLING.

Please enter a word ==> 

Output Specification

Output an introductory message, a prompt for the English word, and a translation for the inputted word.

Use PRECISELY the format below with the EXACT same SPACING and SPELLING. The output below assumes the user entered "Hunger".

This program will convert an English word into Pig Latin.

Please enter a word ==> Hunger

"Hunger"
  in Pig Latin is
"unger-Hay"

Required Methods

// Prompt and read a word to translate
public static String  readWord (Scanner console)

// Convert a word to Pig Latin and return it
public static String  convertWord (String englishWord)

// Return true if "c" is a vowel, false otherwise.
// Handle both lowercase and uppercase letters.
public static boolean isVowel (char c)

// Print result of translation
public static void printResult (String englishWord, String pigLatinWord)

Hints

BEFORE you start coding, think about which methods each method will call. Which methods should "main" call? Create a diagram which shows the calling relationships.

There are many useful String methods listed HERE. You may find startsWith and charAt helpful.

Style

Write comments

  • Choose mnemonic, meaningful variable names (e.g. balance, interestRate)
  • Indent consistently (Ctrl+Shift+F will format your code)
  • Remember the comment block at the top of your program

Solutions

Expert Solution

So the required code has been successfully performed in Java code.

Note:For this program we have used an additional function called substring(). This function with one argument returns the sub string for a string starting from the given index(inclusive) till the end of string .Used as str.substrig(beg-index) where str is the given string and beg-index is the beginning index. This function used with two arguments returns the sub string for a string starting from the first argument(inclusive) till 1 the second argument(exclusive). Used as str.substring(beg-index,end-index) where str is the given string and beg-index is the beginning index and end-index is the ending index.

The following is the program:

import java.util.Scanner;

public class Piglatin {
    public static void main(String args[]){
        String englishWord=readWord(new Scanner(System.in));
        String pigLatinWord=convertWord(englishWord);
        printResult(englishWord,pigLatinWord);
    }
    public static String readWord(Scanner console){
        System.out.println("Please enter a word ==> ");
        String englishWord= console.nextLine();
        return englishWord;
    }
    public static String convertWord(String englishWord){
        String pigLatinWord;
        if(isVowel(englishWord.charAt(0)))
        pigLatinWord=englishWord+"-"+"way";
        else if(englishWord.startsWith("th")||englishWord.startsWith("Th")||englishWord.startsWith("tH")||englishWord.startsWith("TH")){
            pigLatinWord=englishWord.substring(2)+"-"+englishWord.substring(0,2)+"ay";
        }
        else
            pigLatinWord=englishWord.substring(1)+"-"+englishWord.charAt(0)+"ay";
        return pigLatinWord;
    }
    public static boolean isVowel(char c){
        if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
            return true;
        }
        return false;
    }
    public static void printResult(String englishWord,String pigLatinWord){
        System.out.println(englishWord+"\n"+
                            " in Pig Latin is "+"\n"
                            +pigLatinWord);
    }
}

Follow the below images for explanation and indentation:

Output:


Related Solutions

for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
How do I write a COBOL program that translates a word entered into pig Latin?
How do I write a COBOL program that translates a word entered into pig Latin?
Create a program that translates English into Pig Latin. The function will take the first letter...
Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case ​ins​ ensitive. You must write the functiontranslate() ​that takes in a single English word and ​returns​ its Pig Latin translation. Remembertranslatemusttake​onlyonewordasinput.​ ############################################################# # translate() takes a single english word translates it to #pig latin...
Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin...
Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin that doesn't allow follow all the Pig Latin Syntax rules.) Use predefined methods of the Array and String classes to do the work. For simplicity in your conversion, place the first letter as the last character in the word and prefix the characters "ay" onto the end. For example, the word "example" would become "xampleay" and "method" would become "ethodmay." Allow the user to...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which words in English are altered, usually by removing letters from the beginning of a word and arranging them into a suffix. The rules we will use for the pig-latin in this program are as follows: If a word starts with a consonant, split the word at the first instance of a vowel, moving the beginning consonants to the end of the word, following a...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
Pig Latin is a language constructed by transforming English words. The following rules are used to...
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin: *If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”. *If the word begins with a vowel, then “way” is added to the end of the...
python Create a dictionary and insert several English words as keys and the Pig Latin (or...
python Create a dictionary and insert several English words as keys and the Pig Latin (or any other language) translations as values. Write a function called bonus that takes as a parameter a dictionary that has names as keys and salaries as values. Your function should increase everyone’s salary in the dictionary by 5%. Write a function called updateAge that takes as parameters a list of names of people whose birthday it is today, and a dictionary that has names...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT