Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA problem. Create a class PigLatin with a single static method toPigLatin tha

ID: 3568616 • Letter: J

Question

JAVA problem. Create a class PigLatin with a single static method toPigLatin that converts a sentence to PigLatin and returns it. If a word begins with a vowel you add "add" to the end of the word. If the word begins with a consonant you must move the first letter to the end and add "ad". Be sure to move the punctuation to the end of the converted sentence. Write a test program PigLatinTest that gets a sentence from the user and converts it to Pig Latin. You must output the entire sentence translated to Pig Latin. Your program should keep running until the user enters quit(any case).

for example:

This is a sentence.

Histay isway away entencesay.

Explanation / Answer

Here you go :)

//PigLatin class

public class PigLatin {
   private static StringBuffer moded = new StringBuffer();
   public static boolean isVowel(char a) {
       boolean ret = false;
       if ("aeiou".indexOf(a) != -1)
           ret = true;
       return ret;
   }

   public static boolean isConsonant(char a) {
       boolean ret = false;
       if ("bcdfghjklmnpqrstvwxyz".indexOf(a) != -1)
           ret = true;
       return ret;
   }

   public static String toPigLatin(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] + "add";
               }
               else if (isConsonant(words[i].charAt(0))) {
                   words[i] = words[i].substring(1) + "-"+words[i].charAt(0)
                           + "ad";
               }
               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, "");
       }
       return moded.toString();
   }
}

//PigDriver class

//Run this class

import java.util.*;

public class PigDriver {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       String t = "";
       System.out.println("Enter a line(quit to exit): ");
       t=scan.nextLine();
       while(!t.toLowerCase().equals("quit"))
       {
           t = t.toLowerCase();          
           System.out.println(PigLatin.toPigLatin(t));
           System.out.println("Enter a line(quit to exit): ");
           t = scan.nextLine();
       }
   }
}