For this assignment, your job is to create a program that reads in multiple line
ID: 3567559 • Letter: F
Question
For this assignment, your job is to create a program that reads in multiple lines of text, and then produces the translation of that text into the English language variant known as "pig latin".
Pig latin works this way: if a word begins with a vowel (a-e-i-o-u), then "ay" is added to the end of the word (so "idle" -> "idleay", and "often" -> "oftenay"); on the other hand, if a word begins with a consonant, then the first letter is removed, and is placed at the end of the word, followed by "ay" (so "month" -> "onthmay", and "castle" -> "astlecay").
Your program, then, should read in multiple lines of text, ending finally with two carriage returns. After the reading segment ends, your program should then print the pig latin translation of the input text. As a simplification, report the translation with no punctuation, and in all lower case.
Below we give you the driver (the PigDriver class). Your job is to write the Piglatin class so that PigDriver works appropriately.
On this input:
Now is the time,
for all good, and I mean very good men and women,
to visit their grandmothers!
The following output was produced:
ownay isay hetay imetay
orfay allay oodgay anday iay eanmay eryvay oodgay enmay anday omenway
otay isitvay heirtay randmothersgay
Some requirements, and tips:
* Additional helper methods are surely useful; my solution has four methods.
* You will need to use StringTokenizer or the String method split in the Piglatin class.
The StringTokenizer import is provided if you use that class.
Place your Piglatin class here:
import java.util.StringTokenizer;
Explanation / Answer
Here you go :)
public class Piglatin {
private StringBuffer moded = new StringBuffer();
public boolean isVowel(char a) {
boolean ret = false;
if ("aeiou".indexOf(a) != -1)
ret = true;
return ret;
}
public boolean isConsonant(char a) {
boolean ret = false;
if ("bcdfghjklmnpqrstvwxyz".indexOf(a) != -1)
ret = true;
return ret;
}
public void pigConvert(String line) {
if (line.length() > 1) {
String[] words = line.split(" ");
for (int i = 0; i < words.length; i++) {
if (isVowel(words[i].charAt(0))) {
words[i] = words[i] + "ay";
}
else if (isConsonant(words[i].charAt(0))) {
words[i] = words[i].substring(1) + words[i].charAt(0)
+ "ay";
}
moded.append(words[i]+" ");
}
moded.append(" ");
}
for(int i=0;i<moded.length();i++)
{
if(!(isVowel(moded.charAt(i)) || isConsonant(moded.charAt(i)) || moded.charAt(i)==' ' || moded.charAt(i)==' '))moded.replace(i, i+1, ""); //replacing all the other chars like , . ! or ? with null character
}
}
public void pigReport() {
System.out.println(moded);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.