In: Computer Science
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.
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
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: