CS 250 Program 04 Due: Monday, March 5th Main topics: Basic String Methods Boole
ID: 3723168 • Letter: C
Question
CS 250 Program 04 Due: Monday, March 5th Main topics: Basic String Methods Boolean Expressions if & if - else Statements Loop Statements Program Specification: Some of you maybe spoke in Pig Latin when you were younger, so let's try and bring that knowledge back by creating a program that takes a single word of input and then translates it to pig latin. If you are unfamiliar with pig latin, what it entails is finding the first instance of a vowel in a word, moving the entire block prior to that vowel to the end of the word, and finally finishing off the word with "ay". Examples bicycle - icyclebay hello -> ellohay world-> orldway they -> eythay Requirements 1.) The user must first decide whether or not they want to play, and that answer must be validated via a user-validation loop 2.) The word that is converted from Pig Latin MUST be obtained fro the user 3.) Your code must work for all single word inputs Sample run(s): Run Pig Latin Translator! Enter a word and I'1l translate it to Pig Latin! Wanna Play? [y/n]: huh Wanna Play? [y/n]: g Wanna Play? [y/n]: y Give me a word: hello The word in Pig Latin is: ellohay anksThay orfay ayingplayExplanation / Answer
import java.util.Scanner;
public class PigLatin
{
public static void main(String args[])
{
Scanner s =new Scanner(System.in);
System.out.println("Welcome to the Pig Latin Translator !");
System.out.println("Enter a word and I'll translate it o Pig Latin !");
System.out.println("------------------------------------------------");
int count=0;
while(count==0)
{
System.out.print("Wanna play? [y/n] :");
String st=s.next();
st=st.toUpperCase();
if(st.equals("Y")==true)
{
count=1;
System.out.print("Give me a word :");
String str=s.next();
int l=str.length();
int pos=0;String str1="";
for(int i=0;i<l;i++)
{
char ch=str.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
pos=i;
break;
}
else
{
str1=str1+ch;
}
}
String str2=str.substring(pos);
String str3=str2+str1+"ay";
System.out.println("The word in Pig Latin is :"+str3);
System.out.println("anksThay orfay ayingplay");
}
else if(st.equals("N")==true)
{
count=1;
System.out.println("oodGay eByay !");
}
}
}
}
//output
Welcome to the Pig Latin Translator !
Enter a word and I'll translate it o Pig Latin !
------------------------------------------------
Wanna play? [y/n] :huh
Wanna play? [y/n] :g
Wanna play? [y/n] :y
Give me a word :hello
The word in Pig Latin is :ellohay
anksThay orfay ayingplay
Wanna play? [y/n] :y
Give me a word :Bye
The word in Pig Latin is :eByay
anksThay orfay ayingplay
Welcome to the Pig Latin Translator !
Enter a word and I'll translate it o Pig Latin !
------------------------------------------------
Wanna play? [y/n] :n
oodGay eByay !