Question

In: Computer Science

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 nextLine method.

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

Please enter a phrase ==> <user input>
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 "Humpty Dumpty sat on the wall".

This program will convert an English phrase into Pig Latin.

Please enter a phrase ==> Humpty Dumpty sat on the wall

"Humpty Dumpty sat on the wall"
in Pig Latin is
"umpty-Hay umpty-Day at-say on-way e-thay all-way"

************************
Required Methods

// Prompt and read a phrase to translate
public static String readPhrase (Scanner console)

// Convert a phrase to Pig Latin and return it
public static String convertPhrase (String englishPhrase)

// 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 englishPhrase, String pigLatinPhrase)
Hints
You already wrote a method to convert a single word (convertWord). Apply it to each word in the phrase, forming the translated phrase by concatenating the translated words. If you did not get convertWord working on the first lab assignment, you need to get it working now.

Class Scanner can process a String (instead of System.in) consisting of multiple words, and break it into words that are separated by whitespace (tabs, spaces, and newline characters). You know other ways to break a line into words, so you do NOT have to use this technique.

Correctly handle whitespace before and after words.

Solutions

Expert Solution

// Java program to covert a single line english phrase to pig latin phrase
import java.util.Scanner;

public class PigLatin {
  
   // Prompt and read a phrase to translate
   public static String readPhrase (Scanner console)
   {
       String phrase;
       System.out.print("Please enter a phrase ==> ");
       phrase = console.nextLine();
      
       return phrase;
   }
  
   // Convert a phrase to Pig Latin and return it
   public static String convertPhrase (String englishPhrase)
   {
       String pigLatinPhrase = "";
       String words[] = englishPhrase.split(" "); // get the words from the phrase by splitting the input phrase using space as the delimiter
       // loop to convert each word to pig latin and add it to resultant string
       for(int i=0;i<words.length;i++)
       {
           if(i < words.length-1)
               pigLatinPhrase += convertWord(words[i])+" ";
       }
      
       // convert the last word to pig latin
       if(words.length > 0)
       {
           pigLatinPhrase += convertWord(words[words.length-1]);
       }
      
       return pigLatinPhrase;
   }
  
   // Convert a word to Pig Latin and return it
   public static String convertWord (String englishWord)
   {
       // loop over the word
       for(int i=0;i<englishWord.length();i++)
       {
           // if character at index i is a vowel
           if(isVowel(englishWord.charAt(i)))
           {
               // if first character is a vowel, append -way to the word
               if(i == 0)
                   return englishWord+"-way";
               else // else remove and append the substring from the start to the index before vowel and append -ay to the word
                   return(englishWord.substring(i)+"-"+englishWord.substring(0,i)+"ay");
           }
       }
      
       return englishWord+"-ay"; // if word doesn't contain any vowel add -ay at the end of the word
   }
  
   // Return true if "c" is a vowel, false otherwise.
   // Handle both lowercase and uppercase letters.
   public static boolean isVowel (char c)
   {
       // return true if c is a,e,i,o or u in any case
       return("aeiouAEIOU".contains(c+""));
   }

   // Print result of translation
   public static void printResult (String englishPhrase, String pigLatinPhrase)
   {
       System.out.println(englishPhrase+"\nin Pig Latin is\n"+pigLatinPhrase);
   }
  
   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       String englishPhrase = readPhrase(scan);
       String pigLatinPhrase = convertPhrase(englishPhrase);
       printResult(englishPhrase, pigLatinPhrase);
       scan.close();
   }

}
//end of program

Output:


Related Solutions

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...
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. 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...
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...
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...
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?
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...
How would you go about writing a MATLAB program to convert a phrase in Pig Latin...
How would you go about writing a MATLAB program to convert a phrase in Pig Latin to English. I have already written a script that converts phrases from English into Pig Latin; however, I am not sure how to reverse the process. Can anyone who know's MATLAB please help me out, thank you? P.S. I know that this is an unambigious task so it doesn't have to work completely well. With the most minor assumptions made, if it could covert...
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...
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